I have written basic HTML code and php as follows
<html>
<head>
<title>Delete a Record from MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['delete']))
{
$conn = mysql_connect("localhost", "root","");
$Roll_No = $_POST['Roll_No'];
mysql_select_db("d",$conn);
$sql='DELETE FROM student WHERE Roll_No = "' .$Roll_No. '"';
$retval = mysql_query($sql);
if($retval)
{
echo "data deleted successfully\n";
}
else
{
die('Could not delete data: ' . mysql_error());
}
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<th width="150">Student Roll No</t>
<td><input name="Roll_No" type="text" id="Roll_No"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="delete" type="submit" id="delete" value="Delete">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
I have entered data as follows in student table (roll no,
name respectively)
1 Sunil
2 Dhaval
When I run page and if I write either 1 or 2(data which is already there in table) then it deletes successfully
with message deleted data successfully which is ok but
If I write 4 in
textbox then it gives message as deleted data successfully even if there is no
rollno :- 4 instead displaying could not
delete data with error
focus mistake and rewrite code here…