How to output multiple select box array to MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
diotima
Forum Newbie
Posts: 1
Joined: Wed Jul 05, 2006 9:35 pm

How to output multiple select box array to MySQL?

Post 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]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try using implode()
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post by jmut »

grr
Last edited by jmut on Thu Jul 06, 2006 4:57 am, edited 1 time in total.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

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

Post 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?
Post Reply