views:

58

answers:

3

I am programming something like e-mail for my users. I want to allow people to select letters and delete selected. How can I do it? I can't imagine how can I do it only with one MySQL query. Is it even possible?

A: 

It takes one query to list their email, but once you get a list of unique ids that the user has selected, you can run one delete query per email.

Jesse Weigert
+1  A: 

DELETE $table_name WHERE id in ($ids) should do your delete. in one shot. so you jsut need to get all the id's for the records which im assuming are somehow coded into your html as part of the id, class, or value attributes for whatever elements..

prodigitalson
This will work, but it's difficult to parameterize that query properly to avoid SQL injection problems. If you go this route, make sure to sanitize the inputs(make sure every value in ids is the correct type) and also don't forget to restrict the delete query on the correct user so that users don't delete each other's email.Something like:DELETE FROM Email WHERE Email.id IN ($ids) AND Email.user = $username
Jesse Weigert
Jesse, I will do the same with SELECT, using WHERE IN. It is not a problem to check if person deletes his own emails or not :)
hey
+2  A: 

If by "select" and "delete" you mean the SQL commands SELECT and DELETE, then you don't need to SELECT and DELETE at the same time. First run a SELECT to display the emails and to allow the user to choose which emails they want to delete. Then use the ids of the emails (usually a hidden field) to run a DELETE with a WHERE id IN (..., ..., ...).

Mark Byers
That is absolutely what I needed. Thank you very much. ;)
hey
I think Jesse's comment is worth repeating here: remember to check that a user can only delete their own emails. Don't blindly trust the user's list of id's - enforce it by adding an appropriate check in the WHERE clause.
Mark Byers