Deleting a row from a MySQL table using PHP
My queries are not getting deleted from the table. I also want it to return to the first page after deletion.
<html>
<title> Que开发者_运维技巧ries</title>
<body>
<h1> List of Queries</h1>
<form method=post action="delete7.php">
<?php
$ebits = ini_get('error_reporting');
error_reporting($ebits ^ E_NOTICE);// Turns off all the notices &warnings
mysql_connect("localhost","root","") or die(mysql_error());//Connects to the DB
mysql_select_db("testdb") or die(mysql_error()); //Selects one database
echo "<br />";
$query = "select * from queries ";
$result = mysql_query($query) or die(mysql_error()); //sends a unique query to active database on the server
$count=mysql_num_rows($result);
echo "<table border=\"1\">";
echo "<th><tr><td> </td><td>Name</td><td>Address</td><td>ContactNo</td><td>Query</td></tr></th>";
while($row = mysql_fetch_array($result))
{
echo"<tr>";
echo"<td><input type='checkbox' name='Query[]' value=\"".$row['queryId']."\"> </td>";
echo " <td>" . $row['name'] . "</td><td>" . $row['address'] . "</td><td>" . $row['contactNo'] . "</td><td>" . $row['query'] . "</td>";
echo"</tr>\n";
}
?>
<input type="submit" value="Delete" name="Delete"> <br/>
</form>
</body>
</html>
<?php
$conn = mysql_connect("localhost","root","") or die(mysql_error());
$db = mysql_select_db("testdb") or die(mysql_error());
if (isset($_POST['Delete']))
{
foreach ($_POST['Query'] as $checkbox)
{
echo "$checkbox";
$del = mysql_query("DELETE * FROM queries WHERE queryId=
$checkbox") or die(mysql_error());
if($del)
{
echo ("Records Deleted");
}
else
{
echo ("No Way");
}
}
}
?>
Your query to delete data is wrong. You have *
in the delete query which is not allowed.
This should be
$del = mysql_query("DELETE FROM queries WHERE queryId=$checkbox")
or die(mysql_error());
Remove the star from your query:
DELETE FROM queries WHERE queryId = $checkbox;
Didn't you get an error message from mysql_error about a syntax error?
By the way, you cannot call mysql_error without a valid MySQL connection. This means that this row is strange:
mysql_connect("localhost","root","") or die(mysql_error());//Connects to the DB
Consider creating your own error message here, like this:
mysql_connect("localhost","root","") or die('Could not connect to DB');//Connects to the DB
精彩评论