Giant
Giant

Reputation: 1649

deleting multiple entry in database using checkbox php pdo

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

Upvotes: 0

Views: 812

Answers (2)

andyroo
andyroo

Reputation: 394

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

Upvotes: 0

Kevin
Kevin

Reputation: 41885

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
}

Upvotes: 2

Related Questions