how to store multiple checkbox values in 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
amrita2084
Forum Newbie
Posts: 6
Joined: Tue Apr 01, 2008 6:04 am

how to store multiple checkbox values in database

Post by amrita2084 »

Everah | Please use code tags when posting code in the forums.

hi every1...
i would like to know how to store multiple checkbox values in the database using php n mysql and how to retrieve the same value back from the database?
here is the code...if some one can help me..it would b gr8..

Code: Select all

<html><form method="post" action="checkvalues_test.php"> <input type="checkbox" name="colors[]" value="10" /> Red<br><input type="checkbox" name="colors[]" value="100" /> Blue<br><input type="checkbox" name="colors[]" value="1000" /> Green<br><input type="checkbox" name="colors[]" value="10000" /> Yellow<br><input type="submit" value="submit" name="submit"></form></html>
and the php code is

Code: Select all

<?
$ColorsInsert = '*'.implode('*',$_POST['colors']).'*';
mysql_query("INSERT INTO colors(color)      VALUES('$ColorsInsert')")or die (mysql_error());
?>
It is presently storing only 1 checkbox values. i would like to know how to store for multiple checkbox values..pls help me


Everah | Please use code tags when posting code in the forums.
Last edited by RobertGonzalez on Fri Apr 18, 2008 6:53 pm, edited 1 time in total.
Reason: Tags note
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to store multiple checkbox values in database

Post by aceconcepts »

If you are storing the checkbox value as an array in a single field then you would need to serialize the array variable in order to store it and unserialize it to retrieve it from the database.

Otherwise, if you will be storing the checkbox values in seperate fields then you would need to loop the array and extract each value from the array.
amrita2084
Forum Newbie
Posts: 6
Joined: Tue Apr 01, 2008 6:04 am

Re: how to store multiple checkbox values in database

Post by amrita2084 »

thanks for replying . but i dunt know how to do the serialize code...pls if u dunt mind,can u pls wrtie the php code for serialization and retriivng the array values?plsss help me
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: how to store multiple checkbox values in database

Post by onion2k »

Noone is going to write your code for you. Please don't ask people to. We are not here to work for you for free.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: how to store multiple checkbox values in database

Post by aceconcepts »

Look it up in the php manual - it's very simple.

onion2k is right - plus you will learn more by doing it yourself. If you get stuck with anything just post back in this forum and i'm sure you well get good guidance.
amrita2084
Forum Newbie
Posts: 6
Joined: Tue Apr 01, 2008 6:04 am

Re: how to store multiple checkbox values in database

Post by amrita2084 »

hi
thanks for suggesting the php manual....i was jst asking for an example....and not the full code....thanks a lot guys
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: how to store multiple checkbox values in database

Post by califdon »

amrita2084 wrote:thanks for replying . but i dunt know how to do the serialize code...pls if u dunt mind,can u pls wrtie the php code for serialization and retriivng the array values?plsss help me
When you have a question like this, the way to get an answer is to use a search engine. aceconcepts gave you the clue by mentioning serialize and unserialize. Try googling with key words php serialize unserialize.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: how to store multiple checkbox values in database

Post by RobertGonzalez »

Are you wanting to store all of the values in one field or add each value as its own row?
Post Reply