magga Posted January 9, 2007 Share Posted January 9, 2007 Hi I have a page which lists rows from a database.I then have a form where you can select certain rows and delete them.What I want to do is enable users to delete multiple rows from one form which currently it is not doing.These are the bits of code that might be useful in helping me:[code]$notification_id = $_POST['notification_id'];mysql_query("DELETE from notifications where id='$notification_id'");[/code]And from the form:[code]<input type="checkbox" name="notification_id" value="<? echo $row['id'] ?>">[/code](the $id in this comes from another query and does work)It is currently working but you can only delete one row at once obviously because the $notification_id is only holding one value on the post.Do I need to put the values into an array then run the delete query for each value ? If so, how do I go about this ?Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 9, 2007 Share Posted January 9, 2007 Create a "notification_id[]" field (notice brackets) for each record, that will make the field an array.Then change you query as follows:[code]$notification_ids = implode(",",$_POST['notification_id']);mysql_query("DELETE FROM notifications WHERE id IN ({$notification_ids})");[/code] Quote Link to comment Share on other sites More sharing options...
magga Posted February 10, 2007 Author Share Posted February 10, 2007 Can you expand on creating the notification_id[] field ?Do you mean in the form or somewhere else ? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 10, 2007 Share Posted February 10, 2007 Change <input type="checkbox" name="notification_id" value="<? echo $row['id'] ?>">to<input type="checkbox" name="notification_id[]" value="<? echo $row['id'] ?>"> Quote Link to comment Share on other sites More sharing options...
magga Posted February 10, 2007 Author Share Posted February 10, 2007 Thanks a lot, that works now.Can I just ask one thing, does this:[code]WHERE id IN ({$notification_ids})[/code]Just mean WHERE the id's inside the array ? Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 10, 2007 Share Posted February 10, 2007 It's not technically an array. It is a list of comma separated values. But, yes it means where the value exists in the list. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.