Page 1 of 2
[SOLVED] tutorial delete multiple rows code not working
Posted: Sun Oct 22, 2006 2:43 pm
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
Posted: Sun Oct 22, 2006 2:51 pm
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.
Posted: Sun Oct 22, 2006 3:19 pm
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"> </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>
Posted: Sun Oct 22, 2006 3:23 pm
by reecec
doesnt it need to use $_POST but it doesnt it just uses the var $delete
Posted: Mon Oct 23, 2006 8:46 am
by reecec
any ideas or is this code not useable
Posted: Mon Oct 23, 2006 8:55 am
by JayBird
First off
should be (if register globals is off)
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
Posted: Mon Oct 23, 2006 9:06 am
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']).")'";
Posted: Mon Oct 23, 2006 9:09 am
by JayBird
Ooops, my bad, i missed a comma
Code: Select all
$sql = "DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'";
Posted: Mon Oct 23, 2006 9:16 am
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
Posted: Mon Oct 23, 2006 9:26 am
by JayBird
Yes, use mysql_error()
Posted: Mon Oct 23, 2006 9:47 am
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
Posted: Mon Oct 23, 2006 9:49 am
by reecec
Code: Select all
mysql_query("DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'", $link);
echo mysql_error($link);
Posted: Mon Oct 23, 2006 9:55 am
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);
Posted: Mon Oct 23, 2006 9:59 am
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
Posted: Mon Oct 23, 2006 10:11 am
by volka