...

View Full Version : Deleting items that are checked



briintex1
05-21-2008, 05:29 AM
I have looked for this and tried to find examples of what to do and I have not found anything really. I am trying to delete all of the items that are checked with a checkbox. I found a site: http://www.phpeasystep.com/mysqlview.php?id=8

Sorry if I was not supposed to post a link, I figured I would give credit to this person...

I did exactly what they said and it still does not work. Can you look at what I did and tell me what I did?

My code is posted:


<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="password"; // Mysql password
$db_name="information"; // Database name
$tbl_name="test_mysql"; // Table name

// Connect to server and select databse.
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">&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="<?PHP echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><?PHP echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><?PHP echo $rows['name']; ?></td>
<td bgcolor="#FFFFFF"><?PHP echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFFF"><?PHP 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>
<?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=delete_multiple.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

kbluhm
05-21-2008, 06:31 AM
That is a fairly crappy tutorial that relies on register_globals being enabled. It also sends a separate query for every item checked, when it could easily be handled with a single query.

Replace the entire if ($delete) {} block with this:


if ( isset( $_POST['checkbox'] ) && is_array( $_POST['checkbox'] ) )
{
$ids = array_map( 'intval', $_POST['checkbox'] );
$ids = implode( ',', $ids );
$sql = 'DELETE FROM `table_name` WHERE `id` IN ( ' . $ids . ' )';
$result = mysql_query( $sql );
}
else
{
$result = FALSE;
}

...or:


$result = ( isset( $_POST['checkbox'] ) && is_array( $_POST['checkbox'] ) ) ? mysql_query( 'DELETE FROM `table_name` WHERE `id` IN ( ' . implode( ',', array_map( 'intval', $_POST['checkbox'] ) ) . ' )' ) : FALSE;

Same thing, just streamlined. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum