2013-08-16 60 views
0

下面的代码只适用于一个复选框,而忽略其余部分。有什么办法可以让它删除选定的复选框?我一直在使用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

那么表单代码是什么? –

+0

implode部分在哪里?只有一个输入 –

+0

@EmilioGort我现在已经包含了它。 –

回答

4

给复选框的数组式的名字:

<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 in (...)一起使用,但由于元素数目未知,因此无法使用此参数进行替换。所以循环是最好的方式来做到这一点。

+0

谢谢,但它不起作用。不会将其设置为'yes' –

+0

@MikelMax您是否收到相同的错误? –

+0

没有错误,但它没有设置为'yes' –