Page 1 of 1

How to output multiple select box array to MySQL?

Posted: Wed Jul 05, 2006 9:37 pm
by diotima
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm trying to use a multiple select box on a form so that more than one item may be selected and then have the value(s) written to MySQL. I'm able to trace the variables through one screen to the next, but I can't get it to write to the database correctly.

What I have on the form ---

Code: Select all

<snip>

// Select modules from modules table 

$query = "SELECT * FROM $table";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
// Get/Set the ModuleID 
$mid = 'whatever'; 

// create query 
$ModuleNames= "Select * from Modules order by ModuleName asc"; 

// execute query 
$moduleresult = mysql_query($ModuleNames); 


<snip>


<?php
echo '<select name="Modules[]" class="cubufont" multiple="multiple">'; 
while($opt = mysql_fetch_array($moduleresult )) 
{ 

echo '<option value="' . $opt['ModuleID'] . '"'; 

// Select the selected value 
echo ( $opt['ModuleID'] == $mid ) ? ' selected' : ''; 

echo '>'; 
echo $opt['ModuleName']; 
echo '</option>'; 
 }
echo '</select>';   
 
?>
I can get the value(s) to appear on the next page via --

Code: Select all

$Modules=$_POST['Modules'];
   if ($Modules){
    foreach ($Modules as $m){echo '',$m,',';}
   }
But I'm stuck on how to get the value(s) into the MySQL table

I've used the serialize command like this --

$ListModules=serialize($Modules);
And then run the query to write this way --

Code: Select all

mysql_query("INSERT INTO $table (Modules) 
VALUES 
('$ListModules')
But I'm left with the data input looking like this --

Code: Select all

a:2:{i:0;s:2:"71";i:1;s:2:"69";}
What I really want is just to have 71,69 (for example) written to the table. Is this possible? I haven't had any luck with the unserialize.

Thanks,
Marie


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 05, 2006 11:40 pm
by hawleyjr
Try using implode()

Re: How to output multiple select box array to MySQL?

Posted: Thu Jul 06, 2006 4:56 am
by jmut
grr

Re: How to output multiple select box array to MySQL?

Posted: Thu Jul 06, 2006 4:57 am
by jmut
If you don't inted to do any SQL operations on the data I strongly suggest you use serialize/unserialize.

What error you get on unserialize. Why is it impossible?

What mysql type field you store serialized array in?