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
remedy_riderAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
john-formbyConnect With a Mentor Commented:
Hi,

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

Hope this helps,

John
<?php
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());
}
?>
<html>
<head>
<title>PHP / MySQL Implode</title>
</head>
<body>
<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" />
</form>
</body>
</html>

Open in new window

0
 
remedy_riderAuthor Commented:
Thanks!
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.
0
All Courses

From novice to tech pro — start learning today.