Current location: Hot Scripts Forums » Programming Languages » PHP » Help using checkboxes to delete multiple rows from database.


Help using checkboxes to delete multiple rows from database.

Reply
  #1 (permalink)  
Old 12-05-04, 04:49 PM
Cheney Cheney is offline
Newbie Coder
 
Join Date: Dec 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Help using checkboxes to delete multiple rows from database.

I need help using checkboxes to delete mutliple entrys, everything connects right and such, just it keeps saying the array is empty

<FORM ACTION="deletepost.php"><?php

include('db.php');

$q = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $limit");

while($r=mysql_fetch_array($q)){

$news = $r["message"];

$id = $r["id"];

$author = $r["author"];

echo "<FORM ACTION=\"deletepost.php\">

<table border=\"1\" align=\"center\" width=\"760\">

<tr align=\"center\"><td>

<INPUT TYPE=CHECKBOX NAME='delete[]' value ='$id' method= 'post'>Delete a Post!

<P>

<td width = \"500\">$author<br>$news</td></tr></table>
";
}
?>
<p align = "center">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>

deletepost.php:

<?php

include ('db.php');

$post =$_POST['delete'];

echo $post;

foreach($post as $iddelete) {

mysql_query("DELETE FROM news WHERE id='$iddelete'");
}
die("<pre>".print_r($_POST,True));
?>

?>

Warning: Invalid argument supplied for foreach() in /home/cheney/public_html/deletepost.php on line 9

Array
(
)

Thats the error I get
Reply With Quote
  #2 (permalink)  
Old 12-06-04, 03:59 AM
ArksI's Avatar
ArksI ArksI is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Bulgaria
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
why don't you simply use:
1. method="POST"
2.
foreach($_POST[$delete] as $value) {

mysql_query("DELETE FROM news WHERE id='$value'");
}
Reply With Quote
  #3 (permalink)  
Old 12-06-04, 04:29 AM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
if a checkbox is checked, it is "on", else it is ""
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #4 (permalink)  
Old 12-06-04, 04:30 AM
irfanbaba's Avatar
irfanbaba irfanbaba is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Karachi, Pakistan
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up

use following for deletepost.php

********Code Begin here*********
<?php

include ('db.php');

$post =$_POST['delete'];

/*If no entries are checked for deletion, display error*/

if (!$post) { errorMultiple(); exit; }


/*Now Loop through entries and delete checked ones*/
foreach ( $post as $goodbye )

{
mysql_query("DELETE FROM news WHERE id = '$goodbye'");

}


/*Display message*/

multipleDeleted();
exit;


function multipleDeleted() {

Echo "Record(s) Deleted Succesfully.";
}


function errorMultiple() {

Echo "Please select records first.";
}

?>

keep posting if again getting error
__________________
Web Programmer
web: http://www.netxs.com.pk
email: irfanmail@gmail.com
msn : irfanbaba@hotmail.com
Reply With Quote
  #5 (permalink)  
Old 02-14-06, 09:15 AM
mentalray mentalray is offline
New Member
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Cheney you have a html error no PHP,
this part:

<FORM ACTION="deletepost.php"><?php

include('db.php');

$q = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $limit");

while($r=mysql_fetch_array($q)){

$news = $r["message"];

$id = $r["id"];

$author = $r["author"];

echo "<FORM ACTION=\"deletepost.php\">

<table border=\"1\" align=\"center\" width=\"760\">

<tr align=\"center\"><td>

<INPUT TYPE=CHECKBOX NAME='delete[]' value ='$id' method= 'post'>Delete a Post!

<P>

<td width = \"500\">$author<br>$news</td></tr></table>
";
}
?>
<p align = "center">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>

must be like this:

<FORM ACTION="deletepost.php" method= "post"><?php

include('db.php');

$q = mysql_query("SELECT * FROM $table ORDER BY id DESC LIMIT $limit");

while($r=mysql_fetch_array($q)){

$news = $r["message"];

$id = $r["id"];

$author = $r["author"];

echo "<table border=\"1\" align=\"center\" width=\"760\">

<tr align=\"center\"><td>

<INPUT TYPE=CHECKBOX NAME='delete[]' value ='$id' >Delete a Post!

<P>

<td width = \"500\">$author<br>$news</td></tr></table>
";
}
?>
<p align = "center">
<INPUT TYPE=SUBMIT VALUE="Submit">
</FORM>

because you printed the <FORM> tag two times and you are using $_POST[] in your delete PHP file you have to assign post method to the form, no in the checkbox
Reply With Quote
  #6 (permalink)  
Old 02-16-06, 12:56 AM
sham85131's Avatar
sham85131 sham85131 is offline
Newbie Coder
 
Join Date: Oct 2005
Location: india
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
try it.

<input type="checkbox" name="checkbox[]" value="<? echo $row2['prd_code']; ?>" id="checkbox">

just give id field in that tag. see,may it will solve this problem.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Create Multiple Varibles Based On Database cebuy PHP 2 11-25-04 11:15 PM
View, edit, delete and add data to a database bigkid PHP 9 07-21-04 11:51 PM
Help with echo'ing rows of (td's) from database entries paulj000 PHP 6 04-20-04 09:32 AM
can't edit multiple rows at the same time conundrum PHP 2 04-01-04 12:50 AM
asp: validating checkboxes in groups by groupid seala ASP 0 09-08-03 01:36 PM


All times are GMT -5. The time now is 05:18 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.