使用复选框php pdo删除数据库中的多个条目

时间:2014-09-24 08:48:37

标签: php mysql checkbox pdo

我在我的项目中显示一个列表数据来自数据库我使用php pdo获取数据现在我想要一个函数来删除多个条目使用复选框我正在做this教程我正在做约翰的答案,但我得到一个错误说

Warning: PDOStatement::execute() expects parameter 1 to be array, string given in$stmt->execute($id);

这是整个代码

function ImageGalleryDelete(){
    global $dbh;
    if(!empty($_POST['checkbox'])){
        $bigimage = $_POST['checkbox'];
        $stmt = $dbh->prepare("DELETE FROM imagegallery WHERE id = ?");
        foreach ($bigimage as $id)
            $stmt->execute($id);

    }else{
        echo "<script type='text/javascript'>alert('big image is empty');
                window.location='dashboard.php';
                </script>";
                exit;
    }       
}   

为什么我会收到这个错误?任何帮助将不胜感激..

2 个答案:

答案 0 :(得分:2)

提示已在显示的错误消息中,以正确的格式提供数组:

$bigimage = $_POST['checkbox'];
$stmt = $dbh->prepare("DELETE FROM imagegallery WHERE id = :id");
                                                       //   ^ named placeholder
foreach ($bigimage as $id) {
    $stmt->execute(array(':id' => $id));
                // ^ put an key value pair array inside with the designated named placeholder
                // along with the value
}

答案 1 :(得分:0)

我通常会写这个:

$stmt = $dbh->prepare("DELETE FROM imagegallery WHERE id = :id");
foreach ($bigimage as $id) {
    $stmt->execute(array(":id" => $id));
}