繁体   English   中英

使用 PHP 从 MySQL 表中删除一行

[英]Deleting a row from a MySQL table using 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");
    }
  }
}
?>

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

这应该是

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

从您的查询中删除星号:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM