0

I am showing a list in my project the data is from a database i use php pdo to get the data now i want to have a function to delete multiple entry using checkbox i am doing this tutorial i am doing the answer of john, but im getting an error saying

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

this is the entire code

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

why am i getting that error? Any Help will be appreciated..

Community
  • 1
  • 1
Giant
  • 1,619
  • 7
  • 33
  • 67
  • 1
    `why am i getting that error` - because `$id` is not an array. Capt. Obvious. – u_mulder Sep 24 '14 at 08:51
  • so you mean to say i should put it like this `$stmt->execute($bigimage );` bigimage here is array right? – Giant Sep 24 '14 at 08:52
  • 3
    ....... Please read the manual entry for [`PDOStatement::execute()`](http://php.net/manual/en/pdostatement.execute.php) – Madara's Ghost Sep 24 '14 at 08:53
  • 1
    `$bigimage` is array, but it doesn't suit. Read php manual, it's described there - http://php.net/manual/en/pdostatement.execute.php, example 3 explains everything – u_mulder Sep 24 '14 at 08:54

2 Answers2

2

The hint is already in the error message shown, feed an array with the correct format:

$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
}
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Kevin
  • 41,694
  • 12
  • 53
  • 70
0

The way I'd typically write this:

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