Jump to content

Archived

This topic is now archived and is closed to further replies.

diotima

How to output multiple select box array to MySQL?

Recommended Posts

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 ---

[color=blue]<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>'; 

?>[/color]

I can get the value(s) to appear on the next page via --

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

I've used the serialize command like this --

[color=blue]$ListModules=serialize($Modules);[/color]
And then run the query to write this way --

[color=blue]mysql_query("INSERT INTO $table (Modules)
VALUES
('$ListModules')[/color]
But I'm left with the data input looking like this --
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.