正在删除从数据库检索到的web上的表中的行


Deleting rows from table on web retrieved from database

我在从数据库中删除回显到我的网站上的行时遇到问题,我使用了勾选复选框,当选择倍数时,应该删除它们。但这并没有发生!没有从数据库中删除任何内容!请帮忙!

<form method="" action="tester.php">
  <?php 
include 'connect_to_mysql.php';
$count=0;
$count=mysql_num_rows($result);
$result = mysql_query("SELECT * FROM booking ORDER BY ID ASC");
echo "<table border='1'>
<tr>
<th>DEL</th>
<th>Name</th>
<th>Email ID</th>
<th>Phone Number</th>
<th>Collection Address</th>
<th>Collection Date</th>
<th>Collection Time</th>
<th>Make & Model</th>
<th>Message</th>
</tr>";
while($row = mysql_fetch_array($result))
  {
      ?>
           <td align="center" bgcolor="#FFFFFF"><input name="delete_these[]"                                                                                                                         type="checkbox" id="checkbox[]" value="<?php echo $row['ID']; ?>"></td>
   <?php
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['phonenumber'] . "</td>";
  echo "<td>" . $row['collectionaddress'] . "</td>";
  echo "<td>" . $row['collectiondate'] . "</td>";
  echo "<td>" . $row['collectiontime'] . "</td>";
  echo "<td>" . $row['makemodel'] . "</td>";
  echo "<td>" . $row['message'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?> <br>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit"                 id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this 
if(isset($_GET['delete']))  {
for($i=0;$i<$count;$i++){
$id =(int)$_POST['delete_these'][$i];
$sql = "DELETE FROM booking WHERE ID='$id'";
print_r($_GET['delete_these[]']);
$sql = "DELETE FROM booking WHERE id IN($ids)";
echo "<br />SQL: $sql<br />";
$result = mysql_query($sql);
}

if($result){
}
}
mysql_close();
?>
</form>

首先,您只需implode()表单中收集的所有id,然后构建查询。

样本代码:

<form method="POST" action="index.php">
<table>
<?php while($row = mysql_fetch_array($result)): ?>
    <tr>
        <td><input type="checkbox" name="delete_these[]" value="<?php echo $row['id']; ?>" /></td>
        <td><?php echo $row['name']; ?></td>
    </tr>
<?php endwhile; ?>
</table>
<input type="submit" name="delete" value="Delete Selected" />
</form>
<?php
$selected_values = array();
if(isset($_POST['delete'])) {
    $selected_values = $_POST['delete_these'];
    $ids = implode(',', $selected_values);
    $query = mysql_query("DELETE FROM booking WHERE id IN($ids)");
    // this becomes -> delete from booking where id in (1, 2, 3, ...)
}
?>

虽然你仍然可以使用mysqli或PDO,但它无论如何都是免费的