使用PHP从MySQL表中删除一行

时间:2011-06-09 06:12:59

标签: php

我的查询不会从表中删除。我还希望它在删除后返回第一页。

<html>
<title> Queries</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");
    }
  }
}
?>

2 个答案:

答案 0 :(得分:3)

您删除数据的查询错误。您在删除查询中有*,这是不允许的。

这应该是

$del = mysql_query("DELETE FROM queries WHERE queryId=$checkbox") 
       or die(mysql_error());

答案 1 :(得分:3)

从查询中删除星标:

DELETE FROM queries WHERE queryId = $checkbox;

您是否从mysql_error收到有关语法错误的错误消息?


顺便说一下,如果没有有效的MySQL连接,就无法调用mysql_error。这意味着这一行很奇怪:

mysql_connect("localhost","root","") or die(mysql_error());//Connects to the DB

请考虑在此处创建自己的错误消息,如下所示:

mysql_connect("localhost","root","") or die('Could not connect to DB');//Connects to the DB