Delete records in table when the records check box


Dear All:

Anybody can advises me, how can I delete all checked records in table.

below is the script, i wrote to retrieve records from a db and display on a table. when user check on the checkbox beside the records and click delete. It will delete the records.


<?php $dbconnect; $db; $sql_catall; $result = mysql_query($sql_catall); if (!$result) { echo "Could not successfully run query ($sql_catall) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo $row["id"]; echo ""; echo ""; echo $row["category"]; echo ""; echo ""; echo $row["description"]; echo ""; echo ""; echo ""; echo "[Sub-Category] "; echo ""; echo ""; } mysql_free_result($result); ?>




I assume your table displays in a form? If so do the following:

Change your checkbox input field info to the following:

This creates an array and assigns the id value to each checkbox.

Then use a for each loop

if (!empty($cat_checkbox))

foreach ($cat_checkbox as $delete)
mysql_query(“DELETE FROM tablename WHERE id = ‘$delete’”);


Hope that helps!


Thanks for advices. Now I manage to delete the records.

Others, question is before i send to deletecat.php. I would like to check whether user check on the checkbox or not. How can I do that?

I wrote following to excuted delete:

when the button clicked, I will peform:

<!-- function ConfirmDelete(form) {
		alert ("Button Delete Clieck!")
		form.action = "act_deletecategory.php?id=form.cat_checkbox"

Before the alert message box prompt, i would like to check whether user select any records?

furthermore, i would like to have a button for user to check all and uncheck all feature.



There is a clue in the if statement I did has to how to check whether at least one box is checked before deletion. Instead of saying if array isnt empty, check to see if it is:

if (empty($cat_checkbox))
echo “You must specify at least one record!”;

To check or uncheck a series of checkboxes requires a little more javascript. Try this:

  1. Put the following into your tags:
  1. Next you will need to add another checkbox above the checkboxes that display from your database query and call the above function.

It is important that this is inside your form. When you click this checkbox it will highlight the other boxes. When you click it again, it unchecks them

Try that!



My problem is the same…except that it manage to delete it…
but still the it display the notice…

Notice: Undefined index: del_1 in C:ApacheApache2htdocsmedicdoctordel.php on line 10

Notice: Undefined index: del_2 in C:ApacheApache2htdocsmedicdoctordel.php on line 10


<?php include(""); $totalcount=$_POST['totalcount']; #print "$totalcount
"; for($i=1; $i<=$totalcount;$i++){ $newname="del_".$i; #print "$newname
"; $pat_id = $_POST["$newname"]; #print "PAT ID : $patID
"; if ($pat_id!="") { $sql = "DELETE FROM patient WHERE patID = $pat_id"; mysql_query($sql,$conn); } } ?>[/code]

so…any sugestion from anybody??


The undefined variable error is PHP being over sensitive. If you want to mask the error set your error reporting to 0. Put the following at the top of the page that is displaying the error. After your opening php tag should do:


Alternatively, you need to test the state of the variable BEFORE you use it to remove the error. How are you calling the variable?

If you are using a query string, use:

if (isset($_GET[‘del_1’]))
//rest of code

and so on. If you are passing a POST variable, use POST.

Hope that helps.