943,691 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2323
  • PHP RSS
Aug 21st, 2009
0

Delete rows from sql database

Expand Post »
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.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Aug 21st, 2009
0

Re: Delete rows from sql database

DELETE FROM table_name WHERE checkbox = checked?
Reputation Points: 10
Solved Threads: 2
Light Poster
ds2r is offline Offline
27 posts
since Aug 2009
Aug 21st, 2009
0

Re: Delete rows from sql database

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...........
Reputation Points: 9
Solved Threads: 1
Newbie Poster
karuppasamy is offline Offline
14 posts
since Mar 2007
Aug 23rd, 2009
0

Re: Delete rows from sql database

Thanks! I know nothing about java script. Do I just copy and paste that code into my php page?
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Aug 24th, 2009
0

Re: Delete rows from sql database

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.
html Syntax (Toggle Plain Text)
  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:
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Aug 26th, 2009
0

Re: Delete rows from sql database

Well, I tried that, still no luck...
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Aug 26th, 2009
0

Re: Delete rows from sql database

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

PHP Syntax (Toggle Plain Text)
  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. }
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Apr 20th, 2010
-1
Re: Delete rows from sql database
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.....
Reputation Points: 9
Solved Threads: 1
Newbie Poster
karuppasamy is offline Offline
14 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: focus on text box
Next Thread in PHP Forum Timeline: gettext not working under local xampp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC