Inserting CHECKBOX info to MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Inserting CHECKBOX info to MySQL

Post by diseman »

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. : )

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

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);
}

?>
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...
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Inserting CHECKBOX info to MySQL

Post by jraede »

Well, if you want to keep your formatting, just ditch the <select> tags and instead of <option selected="selected"> use <input type="checkbox" checked="checked"/>

Although I'm curious what you are using these for, because adding them as an imploded array to one database column is definitely not the way to go, and also I'm doubting whether adding them as their own column on the same table is the way to go either. What specifically do the checkboxes represent?
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Inserting CHECKBOX info to MySQL

Post by diseman »

Well, in the example I posted above, the CHECKBOXes represent nothing. I was just seeing if I could put two CHECKBOXes on a form, submit them to the db, and then return the data on the same form. Keep in mind, I'm just making a fictitious website to learn PHP.

In my learning website, I have about 15 checkboxes, which simply represented 15 reasons why the user is submitting the form.

Your last response had a great clue that got me looking at the code a little differently and understanding it even better, which was really good. However, still no luck. Now I'm getting a parse error.

Code: Select all

<?
$test_arr['1'] = "car";
$test_arr['2'] = "dog";
?>

<input type="checkbox" name="testing" id="testing" />

<?
foreach ($test_arr as $key => $val)
{
if ($testing == $key)	print "<input type="checkbox" name='".$key." value="checked"'/>";
else 				print "<input type="checkbox" name='".$key."'/>";
}
?>
Right now it seems the ARRAY is all wrong. I'm using a tinyINT(1) in the db for this. Originally in the array I had 'test' and "testing," but that can't be right since I think the value added to the db needs to be a 0 or 1.

Basically, if the CHECKBOX is checked I'd like to send a 1 to the db. Then return a 1 and have the script add -> value="checked" <- to make it show on the form.

Thanks for your reply...
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Inserting CHECKBOX info to MySQL

Post by diseman »

Well, after looking at this for a few hours and finally deciding to back up and just go through it step-by-step and logically, I was able to make it work.

I'm very surprised to find it was so easy after-all. Now I can't figure out why all the examples I've seen made it 10x harder than it actually was. I will post code here shortly.
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

Re: Inserting CHECKBOX info to MySQL

Post by diseman »

Hello,

Here's a little giving back. : )

Basically, I wanted to learn how to create a page that has checkboxes on it, where I can click the checkbox, record it in the MySQL table, and then have it displayed back to me instantly on the same page; or without being taken to another page.

To have it displayed immediately back to me, I simply did the following to my form:

Code: Select all

<form action="" method="post">
I then created three pages; one with the HTML code and two with the php code. The extra (third page) was simply the code for the checkbox, which I INCLUDE in the HTML form. Here it is:

Code: Select all

<?

if ($whatever == 'on')

	print "<input type=\"checkbox\" name=\"whatever\" id=\"whatever\" checked=\"checked\" />" ;

else

	print "<input type=\"whatever\" name=\"divorce\" id=\"whatever\"  />" ;

?>
Just change "Whatever" to whatever your name is for the CHECKBOX and save it in a seperate .php file in some folder (e.g. "_includes"). Then, in your HTML code you can INCLUDE it like this between your <td> to keep it clean:

Code: Select all

<td>  <?php include("../_includes/whatever.php"); ?>  </td>
Now, just create the code to POST your data like you would for any other text field and it should work for you.

One of the tricks I learned was to look into my db table after submitting the checkbox form to see WHAT data was being stored. That proved to be the key because I was thinking it should be one thing when in fact it was storing the word "on" and not something else. Once I realized the stored record was "on," I then changed the code above to reflect the same.

Also, I originally thought the CHECKBOX should be BOOLEAN (0 or 1) and so I created my table in that fashion. That was wrong for me.
Because the form was submitting the word "on" and not a "0" or "1" like I thought, nothing was being stored to the database. Once I changed the table from "whatever TINYINT(1)," to " whatever varchar(64), " I saw the record being saved and was able to fix it all quickly.

Lastly, the examples I found all over the Internet were to store all the CHECKBOX values in ONE table column and then to comma separate them. I didn't like that. I wanted to save each CHECKBOX to it's own db table column, so it was presented a bit nicer to me when looking at the table.

So, just to recap. If you create your .php page to submit data like you normally would for any <input type="text"> and then use my examples above, it should work for you. The secret to making it work for me is ALL in the /_include/whatever.php example shown above and changing the table to VARCHAR(64). By the way, I originally used VARCHAR(64) because I wasn't sure what was being stored, so I wanted enough room to see. However, I think you can make it VARCHAR(2) since there's only one word being stored if the CHECKBOX is checked and that's "on". When it's not checked, nothing is being stored. : )

OK, hope this helps someone...
Post Reply