Please bear with me as I am a newbie…
I have a website with an administration area that will allow the client to add, update and delete “calendar events”. I had a difficult time finding a simple (as I am a newbie) way to delete multiple events using checkboxes, but finally came up with a solution that worked. I want to put this solution out there for two reasons…
-
To help other newbies.
-
to call upon those with greater knowledge to guide us boneheads to a more elegant solution.
One bit of clarification… this is not a website that requires a great deal of security. While I do have a login procedure I have no doubt that this code fails on a number of levels. But it does work, and I welcome any advice in pointing me to the shortcomings to it.
First, the form code to submit events to be deleted…
[php]
$db = mysql_connect(“localhost”, “username”, “password”);
mysql_select_db(“your_database”,$db);
$result = mysql_query(“SELECT * FROM your_table ORDER BY foo”, $db);
if ($myrow = mysql_fetch_array($result)) {
echo "n";
do {
printf("<div>
<p><input type="checkbox" name="delete[" . $rows['id'] . "]" value="%s">%s</p>
<p>%s%s</p>
<p>%s</p>
</div>
n",
$myrow["id"], $myrow["classTitle"], $myrow["classDate"], $myrow["classTime"], $myrow["classDescription"]);
} while ($myrow = mysql_fetch_array($result));
echo "n";
} else {
echo "Sorry, no records were found!";
}
[/php]
Second, the code to process the form…
[php]
$db = mysql_connect(“localhost”, “username”, “password”);
mysql_select_db(“your_database”,$db);
if (isset($HTTP_POST_VARS[‘delete’])) {
foreach ($HTTP_POST_VARS[‘delete’] as $id)
mysql_query(“DELETE FROM your_table
WHERE id
= '” . $id . “’”) or die ('Can’t delete : ’ . mysql_error());
}
else
echo “No items selected”;
[/php]