954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete rows from sql database

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.

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

DELETE FROM table_name WHERE checkbox = checked?

ds2r
Light Poster
27 posts since Aug 2009
Reputation Points: 10
Solved Threads: 2
 

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........... :)

karuppasamy
Newbie Poster
14 posts since Mar 2007
Reputation Points: 9
Solved Threads: 1
 

Thanks! I know nothing about java script. Do I just copy and paste that code into my php page?

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

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.

<input type="checkbox" name="foo[]" value="1">
<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:

foreach($_REQUEST['foo'] as $checkboxVal) {
  // Check the value is a number
  if(is_int($checkboxVal)) $tmpDeleteRecords .= $checkboxVal . ", ";
}
// Remove the last 2 characters (space and ,)
$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.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

Well, I tried that, still no luck...

sfrider0
Junior Poster
149 posts since Oct 2008
Reputation Points: 16
Solved Threads: 0
 

Xan's example should work (except for a few syntax errors).

if ( is_array( $_POST['foo'] ) ) { //you should use post for security
  foreach( $_POST['foo'] as $id ) {
    if ( is_numeric( $id ) ) {
      mysql_query("DELETE FROM `table` WHERE `id` = {$id}") or die(mysql_error());
    }
  }
}
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

in html

ih PHP
foreach($_POST['id'] as $id ) {
mysql_query("DELETE FROM `table` WHERE `id` = $id") or die(mysql_error());
}
enjoy..... :)

karuppasamy
Newbie Poster
14 posts since Mar 2007
Reputation Points: 9
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You