多个复选框删除不起作用

时间:2013-11-11 08:02:10

标签: php mysql checkbox pdo mysqli

我正在尝试使用复选框从表中删除一行。对于所有选中的复选框,全部删除。以下代码不适用于多个复选框。它一次只能删除一行。任何想法?我正在使用pdo显示行并使用mysqli预处理语句执行删除。

<?php
if (isset($_POST['delete'])) 
 {
 if(!empty($_POST['id'])){
 $stmt = $mydb->prepare("DELETE  FROM products where username = ?  and id = ? ");
 echo $mydb->error;
 $stmt->bind_param('ss', $username->username, $id);
 foreach  ($_POST['id'] as $id) {
$stmt->execute();
}

$errors  = "Product Deleted.";
}
}
?>
<form action="" method="post">

<div>
     <input type="submit" value="delete" name="delete">
<?php
 /*
 * Get and/or set the page number we are on
 */
if(isset($_GET['page']))
{
    $page = $_GET['page'];
}
else
{
    $page = 1;
}
$options = array(
    'results_per_page'              => 10,
    'url'                           => 'index.php?page=*VAR*',
    'db_handle'                     => $dbh,
    'using_bound_params'           => true  
);
try
{   
   $paginate = new pagination($page, "SELECT * FROM products where username =  :param_a   order by id desc", $options);
   $paginate->bindParam(':param_a', $username->username, PDO::PARAM_STR, 12);  
   $paginate->execute();

}
catch(paginationException $e)
{
    echo $e;
    exit();
}
/*
 * If we get a success, carry on
 */
if($paginate->success == true)
{
$paginate_result = $paginate->resultset->fetchAll();
foreach($paginate_result as $row)
{
?>
<input type='checkbox' name='id[]'  class='check' value="<?php   echo $row['id'];?>
<?php
echo $row['title'];
echo $row['description'];
}
}?>
</div>
</form>

1 个答案:

答案 0 :(得分:0)

你必须将bind_param放在循环中:

foreach  ($_POST['id'] as $id) {
    $stmt->bind_param('ss', $username->username, $id);
    $stmt->execute();
}