Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox value stored 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
Not sure if this is a mySQL question or if its a PHP question (for the form), but I have a number of checkboxes in my online form and I want to store the values in my mySQL database.

I have a database which stores general information about a user (name, email, tele, city, etc) and I have about 6-8 checkboxes in the form. The user can select any amount of checkboxes.

How do I store these values in my DB?
Do I have a column for every checkbox (which may change) or is there a better method?

Thanks ;-)
 
Depends on what exactly you are storing. By default check boxes are either checked or unchecked nothing more.

It depends on what exactly they mean in either state.

You could have a column for each option. The column type would be boolean.

If you know there might be a change in the number of check box options, then maybe a comma separated text field might make more sense.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yes, I want store the values that get checked.
So if I have several values for developer skills say, like Perl, Java, PHP, etc, I want to store the values that get checked.

I think the answer may be to have one column which stores the values as a comma spearated field would be the answer.
How could I do that?
 
If the number of check boxes is going to be changing or has a chance of changing. Then yes a comma separated list would work.

From the mysql side of things it would be a standard text field.

You can either store, the actual text say like your example of languages PHP,PERL,JAVA or store ones and zeroes. Location would then determine what it refers to.

1,1,0 would mean that PHP and PERL were checked but Java was not.

From the PHP side you would build the list to be inserted.

$lang_field=$checkbox1 . "," . $checkbox2 . "," ...









----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
From the mysql side of things it would be a standard text field.

Or a set. Look at the set data type in the MySQL documentation. I think it most closely matches what you want. In fact, you store the values like in vacunita's string column.

But, as an extra, you can use the FIND_IN_SET() function to check for individual options. Nice if somebody is looking for a PHP job, for example.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
I'm trying to get this work (with an added piece of complexity that I'll come to later!), but I'm not sure I have the syntax correct.

My form is like this:
Code:
<input type="checkbox" name="dept1" value="PHP">PHP&nbsp;<input type="checkbox" name="dept2" value="JAVA">Java&nbsp;<input type="checkbox" name="dept3" value="Perl">Perl

And my form processing is like this:
Code:
$department = ($_POST["dept1","dept2","dept3"]);

is this right?
My column name in my DB is 'department' and I'm expecting that if any of these checkboxes is selected, the vlaue of the checkbox will be stored in the variable and therefore in the DB through my INSERT query.
I can then also use this variable in my email to include the values that are checked, in a comma separated list.

But it doesn't work?? :-(
 
Not correct, you can't address different variables inside the post super global like that. That's just wrong. I would personally do it this way:
Try:
Code:
<input type="checkbox" name="dept[red][][/red]" value="PHP">PHP&nbsp;<input type="checkbox" name="dept[red][][/red]" value="JAVA">Java&nbsp;<input type="checkbox" name="dept[red][][/red]" value="Perl">Perl

This will automatically produce an array with the selected values upon submission of the form. Then all you need to do is implode the array into a comma separated list.

PHP:
if(isset($_POST['dept'])){
$department=implode("[red],[/red]",$_POST['dept']);
}





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Magic!
Worked a treat.
many thanks ...
 
Glad I could help. If you have anymore PHP related questions I suggest you take them to the forum434 here on tek-tips.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I spotted this and had a similar scenario on an access form with a mysql backend. Essentially, I have one table with all of the possible checkboxes and an ID for each checkbox name, and a second table that stores the ID, who selected the option, and when it was selected.

My form gets populated by a query that looks for all the items listed in the first table, and their corresponding ID. Items get ticked, then on submit, all the entries that are ticked get an entry in the second table.

PHP can create dynamic HTML that would get the same effect, but prettier and with a heck of a lot less programming effort than my Access form did.

It is a history, for us we need to store the data for a long time, and it isn't updated or changed often, but I should imagine a high-traffic site would accrue data very quickly using this method.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top