1
$favour_delete=$_GET['favour_delete'];
$favour_delete=implode(",",$favour_delete);

$delete=$db->prepare("DELETE FROM favour WHERE post_id IN (:favour_delete) && user_id=:user_id");
$delete->bindValue(':favour_delete', $favour_delete, PDO::PARAM_STR);
$delete->bindValue(':user_id', $user_id, PDO::PARAM_STR);   

I have a mysql delete multiple row in array. user tick checkbox and send it in array.

I implode it into string and use IN (), I don't know where is went wrong, it only delete one row.

post_id  user_id
2        1
3        1
4        2

So if user: 1 send $favour_delete=array(2,3); it should delete first and second row

9
  • How many records should it delete? Commented Jul 15, 2013 at 15:21
  • depend what user tick, its array, if user tick 3, it should delete 3 row Commented Jul 15, 2013 at 15:22
  • Post a data example that can be sent in your favour_delete. Commented Jul 15, 2013 at 15:24
  • Did you try to print $favour_delete after the implode to make sure it contains 3 values? Commented Jul 15, 2013 at 15:24
  • You can't bind to the IN clause like that, you need to generate the placeholders. See duplicate. Commented Jul 15, 2013 at 15:24

1 Answer 1

0

Unfortunately as of now there is now way to parameterize the in clause in a query your best bet here is as follows.

<?php
$favour_delete=$_GET['favour_delete'];
$favour_delete=array_map('intval',$favour_delete);

$delete=$db->prepare("DELETE FROM favour WHERE post_id IN (".implode(",",$favor_delete).") && user_id=:user_id");
$delete->bindValue(':user_id', $user_id, PDO::PARAM_STR); 
Sign up to request clarification or add additional context in comments.

1 Comment

Why are you using explode on line 2? $_GET['favour_delete']is already an array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.