查询中出错:

时间:2011-05-28 08:38:58

标签: php

当我点击删除按钮时。第二页不删除查询而是说:查询中出错:未选择数据库选定的行已删除。我想删除所选的查询。

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

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

1 个答案:

答案 0 :(得分: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输出,所以如果没有进一步的修改,这将无法工作。