0

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.

5
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by karuppasamy
0

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

Edited by mike_2000_17: Fixed formatting

0

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

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.

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());
    }
  }
}
-1

in html

<input type="checkbox" name="id[]" value="1">
<input type="checkbox" name="id[]" value="2">
<input type="checkbox" name="id[]" value="3">
<input type="checkbox" name="id[]" value="4">
<input type="checkbox" name="id[]" value="5">
ih PHP
foreach($_POST['id'] as $id ) {
    mysql_query("DELETE FROM `table` WHERE `id` = $id") or die(mysql_error());
}

enjoy..... :)

Edited by mike_2000_17: Fixed formatting

Votes + Comments
Please check the date before bumping and replying to an old dead thread.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.