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..

4
  • 1
    why am i getting that error - because $id is not an array. Capt. Obvious. Commented Sep 24, 2014 at 8:51
  • so you mean to say i should put it like this $stmt->execute($bigimage ); bigimage here is array right? Commented Sep 24, 2014 at 8:52
  • 3
    ....... Please read the manual entry for PDOStatement::execute() Commented Sep 24, 2014 at 8:53
  • 1
    $bigimage is array, but it doesn't suit. Read php manual, it's described there - php.net/manual/en/pdostatement.execute.php, example 3 explains everything Commented Sep 24, 2014 at 8:54

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

I'm tempted to downvote you just for the omitted {}.
And the other answer fixed it. You wouldn't leave an SQL injection vulnerability in code, would you? Why leave other glaring bad practices?
I also wouldn't assume $_POST['checkbox'] is sanitized, just a little out of context for the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.