Your code for your form should be...
Code: Select all
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
$sql = "SELECT name
FROM userpic
WHERE username='$activeuser'
LIMIT 4";
if (!$result = mysql_query($sql)) {
die("There was problem with this query: " . mysql_error());
}
while ($code = mysql_fetch_array($userquery)) {
echo '<img border=2 bordercolor=#000000 height=100 width=100 src="userpic/'.$code['name'].'">'.'<input type="checkbox" name="delbox[' . $code['name'] . ']" id="'.$code['name'].'" value="'.$code['name'].'" />';
}
?>
<p><input type="submit" name="Delete" value="Delete">
</form>
One possibility for your delete code could be something like...
Code: Select all
<?php
if (isset($_POST['delbox'])) {
if (is_array($_POST['delbox'])) {
foreach($_POST['delbox'] as $username => $checkdelete) {
if ($checkdelete == 'On') {
$sql = "DELETE FROM userpic WHERE username = '$username'";
$result = mysql_query($sql) or die("Cannot delete $username from the database: " . mysql_error());
if (mysql_affected_rows($result) == 0) {
echo "User $username was not deleted...";
} else {
echo "User $username was deleted...";
}
}
}
}
}
?>
I am sure there is another, more efficient way to do this. The thing to remember when using checkboxes is that the value of the item you are passing needs to be the index of the checkbox in the HTML form. Then, when the form is submitted, the checkboxes become and array, and you need to check which have a value of 'On' (which is checked), then do something with those.
If there is another formfield element you could use, I would maybe try those if you want to make identifying your passed data easier to identify.
Hope this helps.