以下代码仅适用于一个复选框,而忽略其余部分。有没有办法让我删除所选的复选框?我尝试过使用implode
,但它给了我
Warning: implode(): Invalid arguments passed
<form action="" method="post">
<input type="checkbox" name="id" class="check" value = "<?php echo $row['id']; ?>">
<?php echo $row['to_user'];?>
<input type="submit" name="delete">
</form>
<?php
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$ids = implode( ',', $id );
$mydb = new mysqli('localhost', 'root', '', 'database');
$stmt = $mydb->prepare("update messages set deleted = 'yes' where from_user = ? and id = ? ");
$stmt->bind_param('ss', $user, $ids);
$stmt->execute();
echo "Message succesfully deleted";
exit();}
?>
答案 0 :(得分:3)
给复选框一个数组样式的名称:
<input type="checkbox" name="id[]" class="check" value = "<?php echo $row['id']; ?>">
这将使$_POST['id']
成为所有已检查值的数组。然后在循环中删除它们:
$mydb = new mysqli('localhost', 'root', '', 'database');
$stmt = $mydb->prepare("update messages set deleted = 'yes' where from_user = ? and id = ?");
$stmt->bind_param('ss', $user, $id);
foreach ($_POST['id'] as $id) {
$stmt->execute();
}
您不能将逗号分隔的ID列表与=
一起使用。您可以将其与id in (...)
一起使用,但不能使用此参数替换,因为元素的数量未知。所以循环是最好的方法。