我在我的项目中显示一个列表,数据来自数据库,我使用 php pdo 来获取数据,现在我想要一个函数来使用我正在做的复选框删除多个条目 this教程我正在做约翰的答案,但我收到一个错误说
警告:PDOStatement::execute() 期望参数 1 为数组,在
行中给出的字符串 $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;
}
}
为什么我会收到这个错误?任何帮助将不胜感激..
最佳答案
提示已在显示的错误消息中,请使用正确的格式提供数组:
$bigimage = $_POST['checkbox'];
$stmt = $dbh->prepare("DELETE FROM imagegallery WHERE id = <b>:id</b>");
// ^ named placeholder
foreach ($bigimage as $id) {
$stmt->execute(<b>array(':id' => $id)</b>);
// ^ put an key value pair array inside with the designated named placeholder
// along with the value
}
关于php - 使用复选框 php pdo 删除数据库中的多个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26012466/