开发者

There was an error in query:

When i click on the delete button. The second page does not deletes the query instead says: There was an error in the query: No database selected Selected Rows deleted. I want to delete the selected queries.

which code should i include so that second page redirects to the first page?

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&开发者_如何转开发gt;<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");
    }
  }
}
?>


delete4.php should probably be:

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

Please note that there are plenty of improvements to be made.

You were connecting to the database within a foreach which is redundant, and a waste of resources - imagine if you had several thousand items to loop over - that would involve several thousand unnecessary calls being made.

You are also calling mysql_query twice, passing the result of the first query to the second, which will cause problems.

You can use a header('Location:http://site.com/script.php'); to redirect to another page, but only if there is no output prior to the header call. In your script, you have html output, so this will not work without further modification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜