从数据库中检索的Web表中删除行

时间:2014-04-22 01:31:24

标签: php mysql sql database sql-delete

我遇到了从数据库中删除我回到我的网站上的行的问题,我使用了勾选复选框,当选择了多个时,它们应该被删除。但它只是没有发生!什么都没有从数据库中删除!请帮忙!

<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>

1 个答案:

答案 0 :(得分:0)

首先,您可以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,无论如何都是免费的