2014-09-24 43 views
0

我在我的項目中顯示的數據來自數據庫我使用php pdo來獲取數據現在我想要使用複選框刪除多個條目我正在做的功能this教程中,我做約翰的答案,但即時得到一個錯誤說使用複選框刪除數據庫中的多個條目php pdo

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; 
    }  
} 

爲什麼會出現這個錯誤?任何幫助將理解..

+1

'爲什麼我得到那個錯誤' - 因爲'$ id'不是一個數組。上尉明顯。 – 2014-09-24 08:51:08

+0

所以你的意思是說我應該像這樣'$ stmt-> execute($ bigimage);'bigimage這裏的數組是正確的? – Giant 2014-09-24 08:52:27

+3

.......請閱讀手冊條目['PDOStatement :: execute()'](http://php.net/manual/en/pdostatement.execute.php) – 2014-09-24 08:53:39

回答

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 
}
0

我通常寫這個問題的方法:

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

我很想爲您省略的{{}'降級。 – 2014-09-24 09:00:08

+0

它在問題中被省略了。 – andyroo 2014-09-24 09:01:25

+2

而另一個答案解決了它。您不會在代碼中留下SQL注入漏洞,對嗎?爲什麼會留下其他明顯不好的做法? – 2014-09-24 09:02:15

相關問題