Jump to content

Inserting multiple form checkbox results to MySQL database


flipper828

Recommended Posts

Hello All! 

 

I am stuck once more and need your very professional help once again.  For the last two weeks, I have struggled with this 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:

 

 

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!

 

 

Of course, the lines mentioned in the messages differ according to which boxes I check.  The two undefined indexes above are 2 of the 6 boxes I didn't 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
$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;
}

$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</h2><br />\n";      echo "</form>\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!</h2><br /><br />\n";      echo "</form>\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;   
}
?>

 

Any and all guidance will be greatly appreciated.

 

I hope I haven't entered too much information. 

 

Link to comment
Share on other sites

Only checkboxes that are checked are sent in the form data. So, you cannot reference the unchecked checkboxes and expect there to be a value. However, it is pretty easy to write your code to work regardless.

 

But, your code is full of problems. E.g.

$check_list[] = $_POST['check_list[]'];

 

The proper way to reference those checkbox fields is with $_POST['check_list'] (without the []).

 

I would like to provide more help, but I'm not really understanding what you are really doing here and I suspect your approach is totally wrong. From what I see, you have a form that, when posted, creates a new record with two values: the first is 'regid' and the second is the array of the checked values. That really makes no sense to me. How do you differentiate one record vs. another.

 

Plus, I don't see any lines of code in what you posted that would cause the errors you displayed. Are you sure that is the correct code that is processing the form?

 

Anyway, based upon what you have posted, here are my assumptions and recommendations:

 

It looks like this is a form for a person to sign-up for classes. So, first off you need to identify which person the form is being submitted for and include that in the data being stored for the record(s).

 

You should create a table to store the available webinars and those records should have a primary id, available time, and webinar name. You would use that table to generate the form. Then, in that form the checkboxes would be created as arrays with the primary ID of each webinar as the value of the checkbox.

<input type="checkbox" name="check_list[]" value="1" > Wednesday February 29, 2012 4:30 PM EST
<input type="checkbox" name="check_list[]" value="2">Friday March 2, 2012  9:00 AM EST

 

Then, when the user submits the form you would store 1 record for each checkbox selected. The table should have a user_id (or some way to identify the person) and a column for the webinar id as well as any additional information you need on the selections (e.g. timestamp?).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.