Inserting multiple form checkbox results to MySQL database

Hello. I am brand new to this forum and a REALLY wannabe (one day) php programmer. I have completed an introduction and intermediate php/mysql class and have completed several php tutorials. For the last two weeks, I have been searching for an answer to the following problem.

I need the checkbox or checkboxes a user clicks to update the database. Right now, it is recording the boxes checked but I am receiving the following errors on the boxes that are not checked:

[i]Notice: Undefined index: SM_03022012 in C:\wamp\032012SMwebinar\addreg.inc.php on line 21

Notice: Undefined index: FH_03062012 in C:\wamp\032012SMwebinar\addreg.inc.php on line 24

Your information has been successfully received. Thank you![/i]

Of course, the lines mentioned in the messages differ according to which boxes I check.

Here is my table structure.

Table name is smwebinar032012.
regid INT(11) auto-increment primary key
SM_02292012 VARCHAR(50)
SM_03022012 VARCHAR(5)
SM_03072012 VARCHAR(50)
FH_03012012 VARCHAR(50)
FH_03062012 VARCHAR(50)
FH_03082012 VARCHAR(50)

Here is my form on main.inc.php. I’ve removed all the html for easier reading:


<form action=index.php method=post>
  
Webinar 1

<input type="checkbox" name="check_list[]" value="Wednesday February 29, 2012 4&#58;30 PM EST" > Wednesday February 29, 2012 4&#58;30 PM EST

<input type="checkbox" name="check_list[]" value="Friday March 2, 2012  9&#58;00 AM EST">Friday March 2, 2012  9&#58;00 AM EST

<input type="checkbox" name="check_list[]" value="Wednesday March 7, 2012  1&#58;30 PM EST">
Wednesday March 7, 2012  1&#58;30 PM EST      
      
Webinar2

<input type="checkbox" name="check_list[]" value="Thursday March 1, 2012  1&#58;30 PM EST">Thursday March 1, 2012  1&#58;30 PM EST

<input type="checkbox" name="check_list[]" value="Tuesday March 6, 2012  9&#58;00 AM EST">Tuesday March 6, 2012  9&#58;00 AM EST

<input type="checkbox" name="check_list[]" value="Tuesday March 8, 2012  4&#58;30 AM EST">Tuesday March 8, 2012  4&#58;30 AM EST

<input type="submit" value="Submit">
<input type="hidden" name="content" value="addreg">

</form>

And here is my addreg.inc.php file:

[php]<?php

$con = mysql_connect(“localhost”, “jude”,“j001”) or die(‘Sorry, could not connect to database server’);
mysql_select_db(“sales”, $con) or die(‘Sorry, could not connect to database’);

if (!$con)
{
echo “<h2 class=“BodyCopy”>Sorry, we cannot process your request at this time, please try again later\n”;
echo “<a href=“index.php?content=main” class=“BodyCopy”>Please try again
\n”;
exit;
}

$check_list[] = $_POST[‘check_list[]’];

//Everything passed, enter information in database

$result=mysql_query(“INSERT INTO smwebinar032012 VALUES(‘regid’, ‘check_list[]’)”);

$message = 'Invalid query: ’ . mysql_error() . “\n”;
die($message);

if (!$result)

{
echo “<h2 class=“BodyCopy”>Sorry, there was a problem processing your information
\n”;
echo “\n”;

   echo "<table width=\"670\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n";
   echo "<tr>\n";
   echo "<td colspan=\"2\" bgcolor=\"#69670E\" class=\"Copyright\" align=\"center\">© 2012  Inc. All Rights Reserved</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
   
  exit;

} else
{
echo “<h2 class=“BodyCopy”>Your information has been successfully received. Thank you!

\n”;
echo “\n”;

   echo "<table width=\"670\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">\n";
   echo "<tr>\n";
   echo "<td colspan=\"2\" bgcolor=\"#69670E\" class=\"Copyright\" align=\"center\">© 2012  Inc. All Rights Reserved</td>\n";
   echo "</tr>\n";
   echo "</table>\n";
   
  exit;

}

?>[/php]

Any and all guidance will be greatly appreciated.

I hope I haven’t entered too much information.

Hello flipper828, if you have created different different field in database to store check-box value in database than you did not need to give same name to all check box. you can give different name to input check box. Please see below code i have created. [php] <? if($REQUEST_METHOD == 'POST') { echo '
';
	print_r($_POST);
	$SM_02292012 = '';$SM_03022012 = '';$SM_03072012 = '';$FH_03012012 = '';$FH_03062012 = '';$FH_03082012 = '';
	if(isset($_POST['SM_02292012']))
		$SM_02292012 = $_POST['SM_02292012'];
	if(isset($_POST['SM_03022012']))
		$SM_03022012 = $_POST['SM_03022012'];
	if(isset($_POST['SM_03072012']))
		$SM_03072012 = $_POST['SM_03072012'];
	if(isset($_POST['FH_03012012']))
		$FH_03012012 = $_POST['FH_03012012'];
	if(isset($_POST['FH_03062012']))
		$FH_03062012 = $_POST['FH_03062012'];
	if(isset($_POST['FH_03082012']))
		$FH_03082012 = $_POST['FH_03082012'];		
$con = mysql_connect("localhost", "jude","j001") or die('Sorry, could not connect to database server');
mysql_select_db("sales", $con) or die('Sorry, could not connect to database');

if (!$con)
{
   echo "<h2 class=\"BodyCopy\">Sorry, we cannot process your request at this time, please try again later</h2>\n";
   echo "<a href=\"index.php?content=main\" class=\"BodyCopy\">Please try again</a><br>\n";
   exit;
}

$sql = "INSERT INTO smwebinar032012 (regid, SM_02292012, SM_03022012, SM_03072012, FH_03012012, FH_03062012, FH_03082012) VALUES ('', '$SM_02292012', '$SM_03022012','$SM_03072012', '$FH_03012012', '$FH_03062012','$FH_03082012' )";	

$result=mysql_query($sql);
$message = 'Invalid query: ' . mysql_error() . "\n";
if($result)
{
	 echo "<h2 class=\"BodyCopy\">Your information has been successfully received. Thank you!</h2><br /><br />\n";
}
else
{
	echo "<h2 class=\"BodyCopy\">Sorry, there was a problem processing your information</h2><br />\n";
}

}
?>

Webinar 1

Wednesday February 29, 2012 4:30 PM EST

Friday March 2, 2012 9:00 AM EST

Wednesday March 7, 2012 1:30 PM EST

Webinar2

Thursday March 1, 2012 1:30 PM EST

Tuesday March 6, 2012 9:00 AM EST

Tuesday March 8, 2012 4:30 AM EST

[/php] I hope this will helpful for you. Reply your feedback ~~SR~~

Thank you SP. Your help has brought me closer to what I wanted. I was getting pretty desparate when you can to the rescue.

Hello flipper828, it's really nice to see that your problem almost finish. If there is any issue still remain ask. :) ~~SR~~
Sponsor our Newsletter | Privacy Policy | Terms of Service