2011-05-28 93 views
0

当我点击删除按钮。第二页不会删除查询,而是说:查询中出现错误:没有选中数据库选中的行被删除。我想删除选定的查询。查询时发生错误:

我应该包括哪些代码,以便第二页重定向到第一页?

query4.php

<html> 
<title> Queries</title> 
<body> 
<h1> List of Queries</h1> 
<form method=post action="delete4.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> 

delete4.php

<?php 
if (isset($_POST['Delete'])) 
{ 
    foreach ($_POST['Query'] as $checkbox) 
    { 
    echo "$checkbox"; 
    $conn= mysql_connect("localhost","root","") or die(mysql_error()); 
    $sql = mysql_select_db("testdb") or die(mysql_error()); 
    $del = mysql_query("DELETE * FROM queries WHERE queryId=$checkbox"); 
    $rs = mysql_query($del) or die(mysql_error()); 
    if($rs) 
    { 
     echo ("Records Deleted"); 
    } 
    else 
    { 
     echo ("No Way"); 
    } 
    } 
} 
?> 
+5

您需要重新连接到每个页面上的数据库。 – 2011-05-28 08:41:55

+0

如果我使用此代码:<?PHP 如果(isset($ _ POST [ '删除'])) { 的foreach($ _POST [ '查询']为$复选框) { 回声 “$复选框”; $ conn = mysql_connect(“localhost”,“root”,“”)或die(mysql_error()); $ sql = mysql_select_db(“testdb”)或死(mysql_error()); $ del = mysql_query(“DELETE * FROM queries WHERE queryId =”。$ checkbox); $ rs = mysql_query($ del)或die(mysql_error()); if($ rs) { echo(“Records Deleted”); } \t else { echo(“No Way”); } } } ?> – teana 2011-05-29 07:44:50

+0

@teana在评论中很难阅读 - 我想说试试吧 – 2011-05-29 07:46:21

回答

0

delete4.php大概应该是:

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

请注意,有很多的改进作出。

您正在连接到数据库中的一个foreach这是多余的,并浪费资源 - 想象如果你有几千件物品循环 - 这将涉及几千个不必要的呼叫。

您也会打电话mysql_query两次,将第一个查询的结果传递给第二个查询,这会导致问题。

您可以使用header('Location:http://site.com/script.php');重定向到另一个页面,但前提是标头调用之前没有输出。在你的脚本中,你有html输出,所以如果不做进一步的修改就不能使用。

+0

如果你提到改进,你还应该提到数据库输入卫生。 – Arjan 2011-05-29 09:21:31

+0

@ Ross现在说:“你的SQL语法有错误;检查与你的MySQL服务器版本相对应的手册,在第一行'* FROM queries WHERE queryId = 1'附近使用正确的语法。我希望delete4.php应该重定向到query4.php。在这种情况下做什么? – teana 2011-05-30 11:15:40