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?

[Webinar] Streamline your web hosting managementRegister Today

john-formbyConnect With a Mentor Commented:

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

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.
All Courses

From novice to tech pro — start learning today.