Jump to content

How to output multiple select box array to MySQL?


diotima

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.