簡體   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