Delete Multiple Records

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Delete Multiple Records

Post by SpiritWebb »

Ok, I am having a problem. I am wanting to display multiple records in a database, then use a checkbox delete the ones checked. Everything displays just fine. However when I check the box(s) I want to delete, it doesn't delete them. Can anyone help me out?

Code: Select all

<?php
$host="localhost"; 
$username="*****"; 
$password="*******"; 
$db_name="*******"; 
$tbl_name="************"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$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 old metrics from the database</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>Month</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Day</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Year</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>User ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Shift</strong</></td>
<td align="center" bgcolor="#FFFFFF"><strong>Rotation</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Completed</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Total Required</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Total Entered</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Total Removed</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Comments</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"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['datemonth']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['dateday']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['dateyear']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['userid']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['shift']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['rotname']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['rotationcomplete']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['totaltapesreq']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['totalentered']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['totalremoved']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['comments']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php

// Check if delete button active, start this
if(isset($_POST['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=delete_multiple.php\">";
}
else {
  Print "Unable to delete!";
}
}

mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Image

Image
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Delete Multiple Records

Post by Chris »

I haven't read through your code but this is how it should work,
You want to set the check-box's name with []'s after it. This will set is as an array. Then you want to give the value the row id of the database.

Eg.

Code: Select all

$query = mysql_query("SELECT `id` FROM `table`");
while( $array = mysql_fetch_assoc($query) )
{
    echo "<input type=\"checkbox\" name=\"box[]\" value=\"{$array['id']}\" /> {$array['id']} <br />\n";
}
 
Now to loop through the submitted data we loop through $_POST['box']

Code: Select all

if( !empty($_POST['box']) && is_array($_POST['box']) )
{
    foreach( $_POST['box'] as $id )
    {
        // avoid injection
        if( is_numeric($id) )
        {
            mysql_query("DELETE FROM `table` WHERE `id` = '$id'");
        }
    }
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Delete Multiple Records

Post by hallsofvallhalla »

i love Chris....


Your a real asset man.
Post Reply

Return to “Advanced Help and Support”