Fetching from database (a complete example)

MySql Table Structure (newrecords)

Column Type Null Default Comments
ID int(11)
No     
Name varchar(20)
No     
Class varchar(20)
No     

Complete Example

<!DOCTYPE HTML>
<html>
    <head>
        <title>The Complete Example</title>
    </head>
<body>
<p style="font-weight: bold;">Records</p>
<hr>
<form name="form1" action="">
  <p>
    <label for="Name">Name</label>
    <input type="text" name="Name" id="Name">
  </p>
  <p>
    <label for="Class">Class</label>
    <input type="text" name="Class1" id="Class1">
  </p>
    <p>
    <input type="submit" name="Submit" id="Submit" value="Submit">
  </p>
</form>
<p style="font-weight: bold;"> all entered data </p>
<hr>
<?php
$db_server = mysql_connect("localhost", "username", "password");
if (! $db_server)
    die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db("database_Name", $db_server) or
         die("Unable to select database: " . mysql_error());

if ((isset($_GET['Name'])) && (! empty($_GET['Name'])) && (! empty($_GET['Class1'])) && (! empty($_GET['Class1']))) 
{
    
    $query = "INSERT INTO newrecords(Name,Class) VALUES('" . $_GET['Name'] . "','" . $_GET['Class1'] . "');";

    $result = mysql_query($query);
    if (! $result)
    die("Database access failed:  " . mysql_error());
    else
    echo "record inserted";
}

if ((isset($_GET['delete'])) && (! empty($_GET['delete']))) 
{
    $query = " DELETE FROM `newrecords` WHERE ID =" . $_GET['delete'];
    $result = mysql_query($query);
    if (! $result)
    die("Database access failed:  " . mysql_error());
    else
    echo "record deleted";
}

$query = "SELECT * FROM newrecords";
$result = mysql_query($query);
if (! $result)
die("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
if ($rows > 1) 
{
?>
    <table width="100%" border="1">    
<?php
}

for ($j = 0; $j < $rows; ++ $j) {
$row = mysql_fetch_row($result);
?>
 <tr>
		<td><?php echo $row[0]?> </td>
		<td><?php echo $row[1]?> </td>
		<td><?php echo $row[2]?> </td>
		<td><a href="index.php?delete=<?php echo $row[0] ?>">delete</a></td>
		</td>
	</tr> 
<?php
}
?>
</table>
</body>
</html>