I am creating a form of radio buttons, something like :
<dl>
<dt><label for="a1">Meatballs?</label></dt>
<dd>
<input type="radio" value="yes" name="A_1">
</dd>
<dd>
<input type="radio" value="no" name="A_1">
</dd>
<dd>
<input type="radio" value="maybe" name="A_1">
</dd>
</dl>
...
<dl>
<dt><label for="b3">Coffee?</label></dt>
<dd>
<input type="radio" value="yes" name="B_3">
</dd>
<dd>
<input type="radio" value="no" name="B_3">
</dd>
<dd>
<input type="radio" value="maybe" name="B_3">
</dd>
</dl>
<dl>
<dt><label for="b4">Tea?</label></dt>
<dd>
<input type="radio" value="yes" name="B_4">
</dd>
<dd>
<input type="radio" value="no" name="B_4">
</dd>
<dd>
<input type="radio" value="maybe" name="B_4">
</dd>
</dl>
I want to insert thise into a database with a structure like :
INSERT INTO `notes` ( `group`, `data` ) VALUES ...
I am setting some of these values in the processing php like:
$b = "Choices";
// these would be the groups
$bg3 = "Coffee";
$bg4 = "Tea";
// these are the data values
$bp3 = $_POST['B_3'];
$bp4 = $_POST['B_4'];
$b3 = array( $b, $bg3, $bp3 );
$b4 = array ($b, $bg4, $bp4 );
So in my thinking if I need to insert the group value because the 3rd choice had been selected I would have $group = $b3[1]; $data = $b3[2]
In my example there will be about 25 choices with one submit at the end. I need help understanding what would be the most efficient way to loop through all of the radio buttons that are checked and INSERT their values where they need to go?
I need these all to be unchecked and not process unless they have a selection made. Do I need to write insert statements for ever single one?
Thanks to the answer below I now have a more efficient way to get all of the values into an array so using dqhendricks example I can generate an array like this for each set (a,b, etc..) :
print_r($b);
Array ( [0] => Array ( [0] => Choices [1] => [2] => ) [1] => Array ( [0] => Choices [1] => Milk [2] => ) [2] => Array ( [0] => Choices [1] => Water [2] => yes ) [3] => Array ( [0] => Choices [1] => Coffee [2] => ) [4] => Array ( [0] => Choices [1] => Tea [2] => ) [5] => Array ( [0] => Choices [1] => Beer [2] => ) [6] => Array ( [0] => Choices [1] => Wine [2] => ) )
This array was generated from just having the second radio button checked.
UPDATE: Merged my original solution with bqhendrick's one and refactoring your original form to clarify var names and to keep it simpler.
First of all, you should give default values (CHECKED attribute) to radio buttons in order to avoid NULLs and should name radio inputs correctly. Instead of A_1, B_1 and so on, name them like the "group" they refer. Example:
<dl>
<dt><label for="a1">Meatballs?</label></dt>
<dd>
<input type="radio" value="yes" name="Meatballs">
</dd>
<dd>
<input type="radio" value="no" name="Meatballs">
</dd>
<dd>
<input type="radio" value="maybe" name="Meatballs" CHECKED>
</dd>
</dl>
...
<dl>
<dt><label for="b3">Coffee?</label></dt>
<dd>
<input type="radio" value="yes" name="Coffee">
</dd>
<dd>
<input type="radio" value="no" name="Coffee">
</dd>
<dd>
<input type="radio" value="maybe" name="Coffee" CHECKED>
</dd>
</dl>
<dl>
<dt><label for="b4">Tea?</label></dt>
<dd>
<input type="radio" value="yes" name="Tea">
</dd>
<dd>
<input type="radio" value="no" name="Tea">
</dd>
<dd>
<input type="radio" value="maybe" name="Tea" CHECKED>
</dd>
</dl>
After that is done, now is trivial to craft the insert statement just with:
$count=count($_POST);
$counter=0;
$query="INSERT INTO `notes` ( `group`, `data` ) VALUES ";
$resource=mysql_connect('host', 'mysql_user', 'mysql_password');
foreach ($_POST AS $key => $value) {
$key=mysql_real_escape_string($key);
$value=mysql_real_escape_string($value);
$query.="('$key', '$value')";
$query.=($counter<$count-1) ? ", " : ";";
$counter++; //To know whether to put , or ; in SQL sentence
}
//echo $query; //To test it prior to insert for debugging purposes
mysql_query($query);
mysql_close($resource);
And it should do it.
Edited to sanitize a bit $_POST data to avoid posible SQL injection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With