|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
deleting multiple records froma MySql table
At the moment i have a php admin script that allows to delete records from a MySql table. Basically I have all the records listed on screen with a delete option for each record. This is just a link that goes to the delete record script and includes ?id=$id at the end of the link. The record with that unique id number gets deleted then takes you back to the list of records where you can delete another record. However, what I would like to do as there are often lots of records to delete, is to have a check box at the side of each record and a delete button at the bottom of the list. So if i check say 50 records then they would all get deleted which will save me alot of time. Trouble is I have no idea how to go about deleting multiple records and am hoping someone here can help me out. Ideally i'd like to do it with php if possible. Thanks.
|
|
#2
|
|||
|
|||
|
This is not to hard.
![]() On the form you simply assign all id's to the checkbox... (e.g. <input type=checkbox name=to_delete value=" + $id + "> When the form is submitted all the values from seperate checkboxes are assigned comma seperated to the to_delete variable. Simply use the array_todelete = split(to_delete, ",") and all variables are assigned to the dynamical array_todelete use for example ubound to define the amount of items in the array and loop thrue the array deleting the records with the sql query where you use something like array_todelete(count) count = count + 1 Greetz, |
|
#3
|
|||
|
|||
|
[QUOTE=morriscox]It's only not hard if you know how to do it.
I don't know how to implement array_todelete and ubound. And all the docs and ebooks and examples that I've been trying to read have not been very useful.$deleteSQL = "DELETE FROM registered_users WHERE username='".$_POST['delete_user']."'"; $deleteSQL1 = "DELETE FROM users WHERE username='".$_POST['delete_user']."'"; works. Last edited by morriscox : February 25th, 2004 at 01:46 AM. Reason: Realized I posted an URL to a site with sensitive information. |
|
#4
|
|||
|
|||
|
Hi, endly what you need is to name every listed checkbox (with every records) as part of many-index-array like my example below:
<input type=checkbox name='$array[]' value='$records_id'> . In form's target script you have to check the count of indexes of this array ($array[]). $x = 0; while($x<count($array)){ $sql = delete from table_name where id = $array[$x]; if(!sql){ error_function(); } $x++; } FYI: if you have any understood, easy ask about it. Bonoman. |
|
#5
|
|||
|
|||
|
Is there a way to delete the array of records in MySQL so that no looping is needed?
|
|
#6
|
||||
|
||||
|
I'm not positive the following will work in MySQL...
Code:
DELETE FROM table WHERE id IN (1, 2, 3) This should: Code:
DELETE FROM table WHERE id=1 OR id=2 OR id=3 |
|
#7
|
|||
|
|||
|
Ok, thanks. But we still have to loop through the array to get the query.
![]() |
|
#8
|
||||
|
||||
|
Hopefully the first query in my above post works in MySQL... then you could perform the following:
PHP Code:
If you need to use the second query... try this: PHP Code:
Note, both of these are off the top of my head and not guaranteed to work. |
|
#9
|
|||
|
|||
|
Isn't there a problem with using "IN" when an ID number (in the database) is contained in the ID number passed? eg:
IN('345', '678') ... would delete all records with IDs of 3,4,5,6,7,8. Because "3" is IN the string "345" etc. ... |
|
#10
|
||||
|
||||
|
Quote:
I should hope not... but you may want to test that... oh wait... maybe because it's passed as a string, you think? I'm curious now... have you encountered behaviour like this before?
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter! DevArticles Forum Moderator "The net is a waste of time, and that's exactly what's right about it." -- William Gibson |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > deleting multiple records froma MySql table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|