| | |
Delete rows from sql database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 89
Reputation:
Solved Threads: 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.
•
•
Join Date: Mar 2007
Posts: 2
Reputation:
Solved Threads: 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...........
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...........
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.
2. In PHP add something along the line of a foreach to see if the box was checked, something like:
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.
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)
<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:
php Syntax (Toggle Plain Text)
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)
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.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Xan's example should work (except for a few syntax errors).
PHP Syntax (Toggle Plain Text)
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()); } } }
Google is your friend.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
![]() |
Similar Threads
- Delete rows from database (MySQL)
- Using a SP to insert only new rows in SQL Server (MS SQL)
- Change and delete rows in a sql-database (Java)
- SQL Database Problem (C#)
- PLEASE HELP!....SQL Database problem.... (C#)
- conencting to SQL database via VB.net (VB.NET)
- Problem Inserting into SQL database on server (ASP.NET)
- Help with Roles Stored in SQL database (ASP.NET)
Other Threads in the PHP Forum
- Previous Thread: Include, cant make it work
- Next Thread: How can I remove this character •
| Thread Tools | Search this Thread |
ajax apache api array arrays beginner binary broken cache cakephp checkbox class cms code confirm cron curl customizableitems database date display dynamic echo email error external file files folder form forms forum function functions google header headmethod howtowriteathesis href htaccess html iframe image include insert integration ip java javascript joomla limit link login loop mail malfunction menu method mlm multiple mysql neutrality oop paypal pdf php phpmysql play problem query question radio random recursion regex remote root script search select server sessions sms soap source space sql syntax system table tutorial update upload url validator variable video web xml youtube






