Jump to content

Inserting selected checkboxes into db


86Stang

Recommended Posts

I'm displaying a list of dates that the user selects in an earlier form via:

 

<form action="?opt=enter_events" method="post">
<?php
for ($i=strtotime($start_date);$i<=strtotime($stop_date);$i+=86400)
{
	echo "<input type=\"checkbox\" name=\"event_date[]\" value=\"Y\"> " . $event_date;
	echo date('Y-m-d', $i) . "<br />";
}
?>
</form>

 

What I'd like to do is grab any checkbox that is checked and dump them into a table with each being its own record.  Can someone put me on the right track?

Link to comment
https://forums.phpfreaks.com/topic/184315-inserting-selected-checkboxes-into-db/
Share on other sites

thing is, it's almost quicker to just write the code than explain it.

 

"event_date[]" is going to be an array once submitted, so you're going to want to handle it as so:

 

<?php
if (isset ($_POST['submit'])) //your submit button;
{
if (is_array ($_POST['event_date']))
{
	foreach ($_POST['event_date'] as $v)
	{
		$sql = mysql_query ("
			INSERT INTO `your_table`
				(`event_date`)
			VALUES
				('".mysql_real_escape_string($v)."')
		") or trigger_error (mysql_error());
	}
}
else
{ echo 'event_date is not an array.'; }
}
else
{
//form not submitted;
}
?>

 

EDIT: of course alter the query as per your db.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.