Inserting CHECKBOX info to MySQL
Posted: Tue Aug 03, 2010 9:36 am
Hello,
I've reached a point in my form where I have to input CHECKBOX data to my db. I was using a method of coding that worked nicely for the drop-down menu list, but it seems the CHECKBOX is a different animal. What I do know is I do not want to enter the results of my CHECKBOXes into a single column in the db and then comma separate them. Seems everything I see on the Internet for examples wants to do it this way. I prefer to put each result in its own column in the table/db.
Here is some example code that works for me when making drop-down menu lists. I save this as a separate file and then INCLUDE it on the form when I need a drop-down menu. If someone could look at this code and possibly show me how to change it to work with CHECKBOXes, I would be greatful. Please keep in mind that I'm a complete beginner. : )
Here is the PHP code I'm using to submit the form data AND retrieve it at the same time. In other words the form doesn't go anywhere on submission; it stays in place and displays my entries. I think I'm OK here and don't need any help with this. Just wanted to show it in case you find it necessary.
I didn't include the submission form/HTML since all I do is INCLUDE the above code to it.
Because I understand the above and don't want to confuse myself, I'm hoping a/the CHECKBOX solution can be similarly constructed.
Thanks in advance for any help you might be able to provide...
I've reached a point in my form where I have to input CHECKBOX data to my db. I was using a method of coding that worked nicely for the drop-down menu list, but it seems the CHECKBOX is a different animal. What I do know is I do not want to enter the results of my CHECKBOXes into a single column in the db and then comma separate them. Seems everything I see on the Internet for examples wants to do it this way. I prefer to put each result in its own column in the table/db.
Here is some example code that works for me when making drop-down menu lists. I save this as a separate file and then INCLUDE it on the form when I need a drop-down menu. If someone could look at this code and possibly show me how to change it to work with CHECKBOXes, I would be greatful. Please keep in mind that I'm a complete beginner. : )
Code: Select all
<?
$test_select_arr['Select'] = "Select";
$test_select_arr['car'] = "Car";
$test_select_arr['dog'] = "Dog";
?>
<select name="testing" class="dropmenu150" id="testing">
<?
foreach ($test_select_arr as $key => $val)
{
if ($testing == $key) print "<option value='".$key."' selected='selected'>".$val."</option>";
else print "<option value='".$key."'>".$val."</option>";
}
?>
</select>
Code: Select all
<?php
include_once ("../_includes/dbconnect.php");
$testing = "";
if (isset($_POST['submit']))
{
$accident = $_POST['testing'];
$sql = "INSERT learning (testing)
VALUES ('$testing')";
mysql_query($sql,$con) or die("query: $query<br>" . mysql_error());
$id = mysql_insert_id();
$q = "Select * from learning where id=".$id;
$re = mysql_query($q);
if (!$re)
die(mysql_error());
else
{
$ro = mysql_fetch_object($re);
$testing = $ro->testing;
}
mysql_close($con);
}
?>
Because I understand the above and don't want to confuse myself, I'm hoping a/the CHECKBOX solution can be similarly constructed.
Thanks in advance for any help you might be able to provide...