[SOLVED] tutorial delete multiple rows code not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

[SOLVED] tutorial delete multiple rows code not working

Post by reecec »

hi

i have been looking at this tutorial and tried to intergrate this into my code but did not work so i decided to do a test and jus coped the tutorial code into a spearate file

Here is the code:
http://www.phpeasystep.com/mysql/8.html

I can see the table with the data but when i tick the checkboxes and click the submit button it does not delete all it does is refresh

hope somone knows or can see a problem but i dont think this tutorial would have the wrong code

thanks reece
Last edited by reecec on Mon Oct 23, 2006 11:59 am, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

With the actual delete code at the bottom of the script, it makes sense that's what's happening. It's deleting them AFTER it's being displayed in the browser. So right after you see them, they're being deleted.

If you move the section that starts with... if($delete){...} up to the top, you'll get the effect you're looking for.

Although, that is a poorly written tutorial.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Thanks i tried that so as soon as it refreshes it checks the if but still noting happends

New Code:

Code: Select all

<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);

}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=testtheggdel.php\">";
}
else 
{
echo 'error';
}
}

include 'config.php';

$tbl_name="test_mysql"; // Table name


$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?

mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

doesnt it need to use $_POST but it doesnt it just uses the var $delete
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

any ideas or is this code not useable
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

First off

Code: Select all

if($delete){
should be (if register globals is off)

Code: Select all

if(isset($_POST['delete'])){
Secondly, instead of

Code: Select all

for($i=0;$i<$count;$i++){
	$del_id = $checkbox[$i];
	$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
	$result = mysql_query($sql);
I would just do this

Code: Select all

$sql = "DELETE FROM $tbl_name WHERE id IN(".implode("," $_POST['checkbox']).")'";
	$result = mysql_query($sql);
This way only uses one query no matter how many items you delete
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks that looks more like it but it gives me a var error on this part of the code

Code: Select all

$sql = "DELETE FROM $tbl_name WHERE id IN(".implode("," $_POST['checkbox']).")'";
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Ooops, my bad, i missed a comma

Code: Select all

$sql = "DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'";
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks the error has gone


the isset works i have tested this by echoing some text when i submit then i have the sql you gave me but it doesnt delete but definitely reads it

should i add mysql error or do you know why thanks reece
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Yes, use mysql_error()
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

doesnt tell you much
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Code: Select all

mysql_query("DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'", $link);
echo mysql_error($link);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

$query = "DELETE
		FROM
			`$tbl_name`
		WHERE
			`id` IN(" . implode(",", $_POST['checkbox']) . ")'";
$result = mysql_query($query) or die(mysql_error($link). ': '.$query);
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

thanks heres the echoed query
DELETE FROM `test_mysql` WHERE `id` IN(6)'
i clicked on the 6th row so it does get the id

but doesnt delete it

thanks reece
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

")'"
remove the red '
Post Reply