how to store multiple checkbox value into database

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
cylleezz
Forum Newbie
Posts: 3
Joined: Wed Feb 16, 2005 11:46 am

how to store multiple checkbox value into database

Post by cylleezz »

I m stucking with the multiple checkbox that want to store these value into the database.

In my form , that allow the user to select more than one type of languages their like. There are four checkbox
1.)English
2.)France
3.)Mandarin
4.)Others.

In this case, user may select more than one language.So how should i store this value into the database?

my idea is use implode to get the variables into a string, then input that into the database, when you retrieve it you can use explode to get them all back again. Could it be done in this way.

If i need to query from the database which select to show all user that has select only english , how can i make it?
Could anyone give some idea for me.Thank you!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Wrong forum. :roll:

Moved to PHP - Code.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the database of usage is... MySQL ? You can use SET field type, which allows multiple enumerated values to be stored in it.
jonemo
Forum Commoner
Posts: 28
Joined: Wed Feb 09, 2005 1:32 pm
Location: london, uk

Post by jonemo »

why not just one column for each language and fieldtype boolean? it always depends on what you want to do with the data.

just store it -> doesnt matter how you save it
use it -> how do you want to use it?
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

If you just need to store the languages for later use in PHP - you can place the results into an array, serialize the array, and store it on the database in a text field.

Later, grab it from the database, unserialize it - and your original array will be reconstituted for whatever purpose you need it.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you don't want multiple values in one attribute (serialized or not).

probably something like:
table user (user_id, surname, famname, ...)
table userlanguage (user_id, language)

the languages column in userlanguage could be an ENUMeration...


getting all the users that have choosen english goes like

Code: Select all

SELECT *
FROM user
INNER JOIN userlanguage USING (user_id)
WHERE language='EN';
Post Reply