Multiple to one row mysql

Hi guys,
I have a multiple select box I’ve succeeded to insert into my database, but when I insert the data, rows are obviously created for each value inserted. I looked up serialization to try and rearrange the array in a string so only one row is created, but can’t get it.
What am I doing wrong.

code:

<select name="inputfield[]" size=5 multiple="yes">
<option value="tea">tea</option>
<option value="coffee">coffee</option>
<option value="water">water</option>
foreach ($_POST['inputfield'] as $value)
                                        {
                                          $den= serialize($value);
                                          $data = $den;

 mysql_query ("INSERT INTO mainweb (`id`) VALUES('". $data ."')";
}

well, for starters, thinking that you should put multiple values in a string so only one row is created :slight_smile:

better to create multiple rows, that way searching on any given value will be an efficient, indexed query rather than forcing a scan of your entire table

How do I put multiple values in a string when using a select box with multiple values?

I know how to use the foreach function, creating an array but I’m having a hard time converting it into a string. I don’t need multiple rows in my database, not for this field at least.

I agree with Rudy, but if you need the items as a string in the DB, this is an idea:


&lt;?php

if( isset($_POST['inputfield']) ) {
	$array = array();
	foreach( $_POST['inputfield'] as $key =&gt; $value ) {
		$array[] = $value;
	}
	$array = json_encode($array);


// Example insert
	echo "INSERT INTO mainweb (`id`) VALUES('". $array ."') LIMIT 1";
// Example decode	
	print_r(json_decode($array));
}
?&gt;
&lt;form action="test.php" method="post"&gt;
	&lt;select name="inputfield[]" size=5 multiple="yes"&gt;
		&lt;option value="tea"&gt;tea&lt;/option&gt;
		&lt;option value="coffee"&gt;coffee&lt;/option&gt;
		&lt;option value="water"&gt;water&lt;/option&gt;
	&lt;/select&gt;
	&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;

So the insert could look like:
INSERT INTO mainweb (id) VALUES(‘[“coffee”,“water”]’) LIMIT 1

And the decode would be:
Array ( [0] => coffee [1] => water )

Thanks Centered effect, it worked. I hate bothering you again but if or when you have time could you please explain to me what these three lines of your code mean. Because I just copy-pasted it without learning anything.
They did the job, but what does $array = array(); mean or $key => $value???

And how would I echo it out as a normal string. Just plain text that will show beer, tea (assuming I chose those two)?

if( isset($_POST['inputfield']) ) {

    $array = array();

    foreach( $_POST['inputfield'] as $key => $value ) {

        $array[] = $value;

    }
 $array = json_encode($array);

// Example decode    

    print_r(json_decode($array));

Sorry, here is an easier code using implode and explode:
Implode would collapse the array into a string delimited by what you choose. explode will do the opposite

PHP: implode - Manual
PHP: explode - Manual

&lt;?php

print_r($_POST);

if( isset($_POST['inputfield']) ) {
	
	$str = implode(',', $_POST['inputfield']);

// Example insert
	echo "INSERT INTO mainweb (`id`) VALUES('". $str ."') LIMIT 1";
// Example explode	
	print_r(explode(',', $str));
}
?&gt;
&lt;form action="test.php" method="post"&gt;
	&lt;select name="inputfield[]" size=3 multiple="yes"&gt;
		&lt;option value="tea"&gt;tea&lt;/option&gt;
		&lt;option value="coffee"&gt;coffee&lt;/option&gt;
		&lt;option value="water"&gt;water&lt;/option&gt;
	&lt;/select&gt;
	&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;

You can echo it out by echoing $str like what is in the Value part of the SQL statement