MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old November 29th, 2002, 12:05 PM
wap wap is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 20 wap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old December 1st, 2002, 08:27 AM
darkvalley darkvalley is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: Netherland
Posts: 15 darkvalley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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,

Reply With Quote
  #3  
Old February 25th, 2004, 12:12 AM
morriscox morriscox is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Tucson, Arizona, USA
Posts: 1 morriscox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to morriscox Send a message via AIM to morriscox Send a message via Yahoo to morriscox
Smile

[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.

Reply With Quote
  #4  
Old April 12th, 2004, 01:25 PM
Bonoman Bonoman is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 1 Bonoman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #5  
Old July 21st, 2005, 01:25 AM
neuron neuron is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 2 neuron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m
Reputation Power: 0
Is there a way to delete the array of records in MySQL so that no looping is needed?

Reply With Quote
  #6  
Old July 21st, 2005, 07:40 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
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

Reply With Quote
  #7  
Old July 21st, 2005, 07:58 AM
neuron neuron is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 2 neuron User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m
Reputation Power: 0
Ok, thanks. But we still have to loop through the array to get the query.

Reply With Quote
  #8  
Old July 21st, 2005, 08:18 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Hopefully the first query in my above post works in MySQL... then you could perform the following:
PHP Code:
 $query "DELETE FROM table WHERE id IN (".implode(","$array).")"


If you need to use the second query... try this:
PHP Code:
 $query "DELETE FROM table WHERE id IN (".substr(implode(" OR id="$array),4).")"


Note, both of these are off the top of my head and not guaranteed to work.

Reply With Quote
  #9  
Old September 7th, 2006, 01:57 AM
Adam_NZ Adam_NZ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 1 Adam_NZ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 45 sec
Reputation Power: 0
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. ...

Reply With Quote
  #10  
Old September 7th, 2006, 07:45 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 4 m 48 sec
Reputation Power: 8
Quote:
Originally Posted by Adam_NZ
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. ...


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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > deleting multiple records froma MySql table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway