使用复选框删除多个MySQL项目

时间:2014-06-16 06:24:39

标签: php mysql checkbox sql-delete delete-row

我有一个表单,使用户可以选择几个项目:

<table>
<form name="delete" action="proceed.php" method="post">
<?php 
 require_once 'cnn.php';
 $viewPhoto = mysqli_query($cnn, "SELECT * FROM competition WHERE round = 2 ORDER BY id Desc");


                while($row = mysqli_fetch_array($viewPhoto)) {    ?>

<tr><td><input type="checkbox" name="check[]" value="<?php echo $row['id']; ?>"></td>
<td><?php echo $row['id']; ?></td>
<td><img title="" src="<?php echo $row['url']; ?>"  width="178" height="150" /></td></tr>                   

<?php } ?>
<input type="submit" name="delete" value="Delete items">
</form>
</table>

proceed.php然后包括:

<?php
 require_once 'cnn.php';
 $sql = mysqli_query($cnn, "SELECT * FROM competition");

 if(isset($_POST['delete'])){
   foreach ($_POST["check"] as $id){
   $sql = "DELETE FROM soutez WHERE id='$id'";
}
   echo "Items deleted";
}
?>

但是,不会从数据库中删除这些项目。哪里可能是错误?

1 个答案:

答案 0 :(得分:4)

您缺少在循环中执行实际查询以删除记录

<?php
 require_once 'cnn.php';
 $sql = mysqli_query($cnn, "SELECT * FROM soutez");

 if(isset($_POST['delete'])){
   foreach ($_POST["check"] as $id){
   $sql = "DELETE FROM soutez WHERE id='$id'";
   //Here you are missing below statement
   mysqli_query($cnn, $sql);
}
   echo "Items deleted";
}
?>