Page 1 of 1

how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 6:27 am
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.

Re: how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 6:31 am
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.

Re: how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 6:36 am
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

Re: how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 6:40 am
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.

Re: how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 7:58 am
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.

Re: how to store multiple checkbox values in database

Posted: Thu Apr 17, 2008 10:25 pm
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

Re: how to store multiple checkbox values in database

Posted: Fri Apr 18, 2008 6:51 pm
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.

Re: how to store multiple checkbox values in database

Posted: Fri Apr 18, 2008 6:54 pm
by RobertGonzalez
Are you wanting to store all of the values in one field or add each value as its own row?