views:

124

answers:

6

Is there any way i could run the following 'logical code' to actually work?

$sql=mysql_query("DELETE FROM users WHERE id='3,4,5,9'");

I basically want to give my user a tick box to tick for all displayed rows, they can then pick which ones to remove... i just want to remove more than one row with the id's specified?

Any ideas?

+13  A: 

You can use the IN operator for that.

DELETE ... WHERE id IN (3,4,5,9)

VolkerK
+2  A: 

You can use "in" instead of "=".

eg.

DELETE FROM users WHERE id IN (3,4,5,9);
Jarod Elliott
+5  A: 

What about :

$sql=mysql_query("DELETE FROM users WHERE id IN (3, 4, 5, 9)");

Provided :

  • your ids are numeric in the DB
  • you want to delete user 3, and delete user 4, and delete user 5, and delete user 9

And, of course, if your ids are strings :

$sql=mysql_query("DELETE FROM users WHERE id IN ('3', '4', '5', '9')");

For more informations, see :

Pascal MARTIN
The arguments to `IN()` don't have to be numeric. They just have to be values.
Bill Karwin
@Bill : what I meant is "don't use quotes if numeric, as those would not be needed -- I've eddited my post to add the same example considering ids are strings, hoping it'll make my words more clear)
Pascal MARTIN
Yes, thanks for the clarification. I had given you +1 already anyway. :-)
Bill Karwin
+3  A: 

Use a WHERE IN clause.

DELETE FROM users WHERE id IN (3, 4, 5, 9);
Duncan Beevers
+2  A: 

Have you tried using IN

DELETE FROM users WHERE id IN (3,4,5,9)

Although in my applications I never delete anything. Instead I have an active flag that I set to false.

UPDATE users set active=0 where id in (3,4,5,9)

All queries then have a where clause for active=1 and 1 is the default value for the active flag in the table.

pjp
+2  A: 

$sql=mysql_query("DELETE * FROM table WHERE id IN (3,4,5,9)");

nandocurty