View Full Version : Deleting items that are checked

05-21-2008, 06: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:

$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";


<table width="400" border="0" cellspacing="1" cellpadding="0">
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
<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>
<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>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
// Check if delete button active, start this
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);

// if successful redirect to delete_multiple.php
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";

05-21-2008, 07: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 );
$result = FALSE;


$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. ;)