Delete rows from sql database

Reply

Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training

Delete rows from sql database

 
0
  #1
Aug 21st, 2009
I have several columns in each row stored with a user's information. I have put a checkbox at the end of each row and a Delete button at the at the bottom of the table. How can I check a box and simply delete the whole row from the database? Possibly check multiple boxes and delete them all? I have looked up tutorials on this but never had any luck.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 5
Reputation: ds2r is an unknown quantity at this point 
Solved Threads: 0
ds2r ds2r is offline Offline
Newbie Poster

Re: Delete rows from sql database

 
0
  #2
Aug 21st, 2009
DELETE FROM table_name WHERE checkbox = checked?
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: karuppasamy is an unknown quantity at this point 
Solved Threads: 0
karuppasamy karuppasamy is offline Offline
Newbie Poster

Re: Delete rows from sql database

 
0
  #3
Aug 21st, 2009
1. pass all record ids in the check box

2. use java script

var recordID= function(){
var a = "";
for(var i=0; i < document.formname.checkboxID.length; i++){
if(document.ks.checkboxID[i].checked == true){
a = a+documentformname.checkboxID[i].value+",";
}
document.formname.action = "filename.php?a=" + a;
}
call this function in submit button
3. PHP code

if(isset($_REQUEST['a']) != '') {
$a = $_REQUEST['a'];
$d=split(",",$a);
$did=array_filter($d);
foreach($did as $i => $value) {
$D = $value;
mysql_query("DELETE FROM table.name where recordid='".$D."' ")or die(mysql_error());
}
unset($did);unset($a);
}
ok thats all...........
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training

Re: Delete rows from sql database

 
0
  #4
Aug 23rd, 2009
Thanks! I know nothing about java script. Do I just copy and paste that code into my php page?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Delete rows from sql database

 
0
  #5
Aug 24th, 2009
If you are going to use karuppasamy's example, I would make sure you add some protection, check that the values passed are actually numbers. Otherwise you may find that your database is open to attack.

Also, JavaScript is overkill here, no point using it when the same can be acheived simply using HTML and PHP.
1. Make sure all the checkboxes have the same value for name and add [] to indicate it is an array to PHP and set the value to the id of the database row. e.g.
  1. <input type="checkbox" name="foo[]" value="1">
  2. <input type="checkbox" name="foo[]" value="2">

2. In PHP add something along the line of a foreach to see if the box was checked, something like:
  1. foreach($_REQUEST['foo'] as $checkboxVal) {
  2. // Check the value is a number
  3. if(is_int($checkboxVal)) $tmpDeleteRecords .= $checkboxVal . ", ";
  4. }
  5. // Remove the last 2 characters (space and ,)
  6. $deleteRecords = $substr_replace($tmpDeleteRecords,"",-2)
You can then use the returned $deleteRecords in your SQL query to. Although the code above is untested and from memory, so you will need to review before using.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training

Re: Delete rows from sql database

 
0
  #6
Aug 26th, 2009
Well, I tried that, still no luck...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: Delete rows from sql database

 
0
  #7
Aug 26th, 2009
Xan's example should work (except for a few syntax errors).

  1. if ( is_array( $_POST['foo'] ) ) { //you should use post for security
  2. foreach( $_POST['foo'] as $id ) {
  3. if ( is_numeric( $id ) ) {
  4. mysql_query("DELETE FROM `table` WHERE `id` = {$id}") or die(mysql_error());
  5. }
  6. }
  7. }
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC