Mutiple Checkboxes to Update/Delete MYSQL Database with PHP

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…

  1. To help other newbies.

  2. 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]

How about something like…

$result = mysql_query("SELECT * FROM your_table ORDER BY foo", $db); 
if ($result)
{
   echo "n";
   for ($i = 0; $i < mysql_num_rows($result); $i++)
   {
       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"]);   
   }
   echo "n";
   if ( mysql_num_rows($result) == 0)
   {
      echo 'Sorry, no records were found!';
   }
}
else
{
   echo'Error with the query.  The error is' . mysql_error();
}

The rest looks good to me but you might wish to consider your coding style. Granted technically it is correct but it can be hard to read if you don’t know the language well. by indenting if statements your code becomes much easier to read and debug. And one more small thing to consider… Your DB info. It is generally considered better (for security reasons) to include the database variable info rather then have it hard coded into your code.

Unfortunately, that results in a Parse error somewhere in this area…

if ($myrow = mysql_fetch_array($result)) {

	  echo "n";

Thanks for the comments. In the future I will include the database connection info and work on my coding style.

Are you talking about a parse error in the code I gave you? I wouldn’t be surprised… that was off the top of my head so it would need cleaning up. It was just given as an example of another way to handle the problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service