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

user2178521
  • 833
  • 1
  • 14
  • 26
  • How many records should it delete? – Hituptony Jul 15 '13 at 15:21
  • depend what user tick, its array, if user tick 3, it should delete 3 row – user2178521 Jul 15 '13 at 15:22
  • Post a data example that can be sent in your `favour_delete`. – DontVoteMeDown Jul 15 '13 at 15:24
  • Did you try to print `$favour_delete` after the implode to make sure it contains 3 values? – Mash Jul 15 '13 at 15:24
  • You can't bind to the IN clause like that, you need to generate the placeholders. See duplicate. – MrCode Jul 15 '13 at 15:24
  • yes i did print it, and it contain all value – user2178521 Jul 15 '13 at 15:25
  • Parameters to prepared statements can only be literals, whereas the `IN()` operator takes one or more comma-separated values. What you're effectively creating is an expression `WHERE post_id IN ('2,3,4')` which is emphatically *not* the same as `WHERE post_id IN (2,3,4)` (which you want). See the duplicate question to which @MrCode linked for possible solutions. – eggyal Jul 15 '13 at 15:27
  • @user2178521: Not sure what you mean by "*loop bind*". Why don't you try reading the question linked above? – eggyal Jul 15 '13 at 15:29
  • The others who noted that you can't do this the way you are thinking are correct. What you are in essence sending is `IN('1,2,3')` (i.e a string). I would also suggest that you really should be using POST any time that you want to change data in your system, you don't want people to be able to deletee data based on coming to a bookmarked page, navigating to a page from browser history, etc. – Mike Brant Jul 15 '13 at 15:29

1 Answers1

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); 
Orangepill
  • 24,500
  • 3
  • 42
  • 63