Adding multiple values into one field using mysql

Hello, I am fairly new to php and mysql. I noticed that when taking values from a form and inserting into mysql, it will not take multiple values and comma sperate them. in ms sql I had a form for example. "Which colors do you like" with check boxes red, green, blue, yellow, black
It would put them in the field "perfered_colors" as comma sperated. In my mysql insert it just puts one of the values? I'm sure this is an easy solution
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


You can do this by using implode.  Have a look at the following example.

Hope this helps,

if(isset($_POST['submit'])) {
	foreach($_POST as $key=>$value) { 
        $$key = $value; 
	$prefered_colors = implode(',',$colour);
	$query = mysql_query("INSERT INTO yourtable (prefered_colors) VALUES ('$prefered_colors')") or die(mysql_error());
<title>PHP / MySQL Implode</title>
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
Prefered Colors:
Red: <input type="checkbox" name="colour[]" value="red" />
Green: <input type="checkbox" name="colour[]" value="green" />
Blue: <input type="checkbox" name="colour[]" value="blue" />
Yellow: <input type="checkbox" name="colour[]" value="yellow" />
Black: <input type="checkbox" name="colour[]" value="black" />
<input type="submit" name="submit" value="Submit" />

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
remedy_riderAuthor Commented:
I'll give that a shot.

 I thought it could be as easy as a line of code, but i guess there are diferences going from sql to mysql.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.