I have fieldset with multiple checkboxes for a MySQL insert from a form.
<fieldset id="my_checkboxes">
<legend>My Checkboxes (Select at least 2)</legend>
<label for="checkbox1">
<input type="checkbox" id="checkbox1" value="yes" name="the_checkbox" />
CheckBox #1
</label>
<label for="checkbox2">
<input type="checkbox" id="checkbox2" value="yes" name="the_checkbox2" />
CheckBox #2
</label>
</fieldset>
I need to be able to have each checkbox have a unique NAME attribute because each checkbox inserts data into it’s own column in the database. (Otherwise, i would just use the the_checkbox[] as the name for each).
My problem is I need to have the user select at least 2 checkboxes in order to submit the form. Since I have seperate names for each, I cannot use the following code anymore as it counts by name:
$checkboxcount = count($_POST['the_checkbox']);
if($checkboxcount < 1)
{
$errors[] = $checkboxcount;
}
MY SQL statement looks like this:
$sql = "INSERT INTO $db_table(the_checkbox,the_checkbox2) values
(
'".mysql_real_escape_string(stripslashes($_REQUEST['the_checkbox']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['the_checkbox2']))."'
)";
What would you suggest for me to either A. Count checkboxes even with seperate Names, or B. keep the same name but somehow insert data into MySQL as seperate column names?
If you prefer to keep different names, then do something like this:
I would usually prefer to use use the same name for a related group of checkboxes though, with different values, but the way you are handing them directly into a SQL string makes your method of different names a little easier to handle.