数据库
首页 > 数据库> > php-使用复选框删除多个MySQL项目

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

作者:互联网

我有一个允许用户选择几个项目的表格:

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

然后,procedure.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";
}
?>

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

解决方法:

您缺少循环执行删除记录的实际查询

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

标签:delete-row,sql-delete,mysql,php,checkbox
来源: https://codeday.me/bug/20191029/1960026.html