1

I'm trying to delete multiple records in a database based on selections from check boxes in a form. But if multiple check boxes are selected, the current sql only deletes first id for some reason, and not deleting the rest.

$del_ids = implode(',', array_keys($_POST['check_list'])); 
$query = "DELETE FROM enhancements WHERE id IN('$del_ids')";
$result = mysqli_query($connection, $query);

For example my array looks like this:

Array
(
    [check_list] => Array
        (
            [4] => on
            [6] => on
        )
)

And after running:

$del_ids = implode(',', array_keys($_POST['check_list']));

my $del_ids string looks like this:

4,6

But for some reason only record with id 4 deletes, and record with id 6 does not delete and they should both delete. Any suggestions?

3
  • Are you sure del_ids contains multiple values? Commented Mar 26, 2015 at 11:36
  • 2
    it seems you are quoting the IN values , resulting in something like IN ('4,6') Commented Mar 26, 2015 at 11:37
  • 1
    Change WHERE id IN('".$del_ids."') to WHERE id IN(".$del_ids.") Commented Mar 26, 2015 at 11:38

3 Answers 3

2

you have to use this:

$del_ids = implode("','", array_keys($_POST['check_list']));

Sign up to request clarification or add additional context in comments.

Comments

0
$query = "DELETE FROM enhancements WHERE id IN('".$del_ids."')";

sends keys like a string, try replace with

$query = "DELETE FROM enhancements WHERE id IN(".$del_ids.")";

and it should work

Comments

-1

either run muliple delete statements, or append OR clauses in your statement.

The array you have with the ids to delete needs to be enumerated, you could go with something like this :

        foreach($_POST['check_list']as $key) {
    $query = "DELETE FROM enhancements WHERE id IN('".$key."')";
$result = mysqli_query($connection, $query);
    }

1 Comment

and the fact that you're using as $key instead of $key => $value, you'll get those on values instead of the actual key, this will yield IN('on')

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.