通过复选框删除多行


deleting multiple rows via checkbox

我正试图使用复选框删除多行,但我似乎没有成功。有人能告诉我我缺少什么吗?或者有什么更好的方法?

我正试图使用它们的id删除行。我收到以下通知,在删除的列中没有任何内容设置为yes

Notice: Array to string conversion in

<form action="" method="post">
<?php
$result = $stmt->get_result();
echo"<div class='message'>";
while ($row = $result->fetch_assoc()) {
?>
<div class='msgwrap'>
<input name="checkbox[<? echo $row['id']?>]" type="checkbox" ><?php echo $row['to_user'];?>
</div>
<?php
}
?>
<input type="submit" name="delete">
</div>
</form>
<?php
if (isset($_POST['delete'])) {
$id = $_POST['checkbox'];  
$mydb = new mysqli('localhost', 'root', '', '');
$stmt = $mydb->prepare("update messages SET deleted ='yes' where to_user = ? and id = ? ");
$stmt->bind_param('ss', $user, $id);
$stmt->execute();}
?>

PHP会自动将复选框的POST值转换为数组,因为您在name属性中使用了数组表示法。试试这样的东西:

$ids = implode( ',', $id );
$stmt = $mydb->prepare("update messages SET deleted ='yes' where to_user = ? and id IN(?) ");
$stmt->bind_param('ss', $user, $ids);

从您的代码中还不清楚$user是从哪里来的。