[SOLVED] tutorial delete multiple rows code not working
Moderator: General Moderators
[SOLVED] tutorial delete multiple rows code not working
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
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.
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.
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.
Thanks i tried that so as soon as it refreshes it checks the if but still noting happends
New Code:
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>First off
should be (if register globals is off)
Secondly, instead of
I would just do this
This way only uses one query no matter how many items you delete
Code: Select all
if($delete){Code: Select all
if(isset($_POST['delete'])){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);Code: Select all
$sql = "DELETE FROM $tbl_name WHERE id IN(".implode("," $_POST['checkbox']).")'";
$result = mysql_query($sql);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']).")'";Ooops, my bad, i missed a comma
Code: Select all
$sql = "DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'";Code: Select all
mysql_query("DELETE FROM `$tbl_name` WHERE `id` IN(".implode(",", $_POST['checkbox']).")'", $link);
echo mysql_error($link);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);