2011-06-09 133 views
0

我的查詢沒有從表中刪除。我也希望它在刪除後返回到第一頁。使用PHP從MySQL表中刪除行

<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"); 
    } 
    } 
} 
?> 

回答

3

您的查詢刪除數據是錯誤的。您在刪除查詢中有*,這是不允許的。

這應該是

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

它可以幫助把正確的查詢,太:'DELETE FROM查詢的WHERE queryId = $ checkbox'(DELETE後沒有'*')。另外,如果你看到這樣的代碼,請解釋一下SQL注入... – Konerak 2011-06-09 06:17:26

+2

Konerak,你爲什麼不自己解釋SQL注入? :-) – 2011-06-09 06:19:00

+0

謝謝:) :)。我只需要移除這顆星星。你能告訴我哪個coe應該補充說第二頁返回到刪除後的第一頁 – teana 2011-06-10 05:13:35

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