Delete item when more than one checkox is selected
Posted: Wed Nov 19, 2003 7:03 am
Have a piece of code that deletes an entry from a database when a checkbox is selected and the submit button is pressed, using a form. However it will only delete one checkbox, the last one that is selected. My code is. In the form to make checkbox:
<?php
$query = "SELECT user_id, username, CONCAT(first_name,' ',last_name) AS name FROM users ORDER BY first_name ASC";
$result = @mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<TR><TD align=\"left\" bgcolor=\"#c0c0c0\"><input type = \"Checkbox\" name=\"UserDelete\" value={$row['user_id']}>{$row['name']} ({$row['username']})<br></td></td>";
}
mysql_close();
?>
And to handle the post:
if (!empty($_POST['UserDelete'])) {
$UserD = escape_data($_POST['UserDelete']);
} else {
$UserD = FALSE;
echo '<p>You have not selected a user to delete<p>';
}
if ($UserD) {
$query="DELETE FROM users WHERE user_id='$UserD'";
$result = @mysql_query( $query );
if (!$result)
{
echo 'cannot run query';
exit;
}
else
echo '<p>has been deleted from the system</p>';
}
}
If anyone has any suggestions to how i could make the code delete every checkbox that a user presses i would be grateful.
<?php
$query = "SELECT user_id, username, CONCAT(first_name,' ',last_name) AS name FROM users ORDER BY first_name ASC";
$result = @mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<TR><TD align=\"left\" bgcolor=\"#c0c0c0\"><input type = \"Checkbox\" name=\"UserDelete\" value={$row['user_id']}>{$row['name']} ({$row['username']})<br></td></td>";
}
mysql_close();
?>
And to handle the post:
if (!empty($_POST['UserDelete'])) {
$UserD = escape_data($_POST['UserDelete']);
} else {
$UserD = FALSE;
echo '<p>You have not selected a user to delete<p>';
}
if ($UserD) {
$query="DELETE FROM users WHERE user_id='$UserD'";
$result = @mysql_query( $query );
if (!$result)
{
echo 'cannot run query';
exit;
}
else
echo '<p>has been deleted from the system</p>';
}
}
If anyone has any suggestions to how i could make the code delete every checkbox that a user presses i would be grateful.