Forum Moderators: coopster

Message Too Old, No Replies

Choosing multiple selections from form

and instering to mysql

         

blue_eagle

9:40 pm on May 18, 2006 (gmt 0)

10+ Year Member



Hi,

I am having problems with inserting multiple selections from my form to my mysql database. The code i use to enter only one data from my drop down box works fine and the option that i choose is easily insterted to my mysql database. However, multiple selections doesn't seem to work. Here are the codes i try to enter multiple selections to database.

form code:

<td class="bodytext" valign="top" align="left"><b>Control
Panel(s)</td>
<td class="bodytext"><select size="5" name="control_panel" multiple tabindex="¦">
<option value="Not Specified">Not Specified</option>
<option value="Cpanel">Cpanel</option>
<option value="Cpanel/WHM">Cpanel/WHM</option>
<option value="Plesk">Plesk</option>
<option value="Ensim">Ensim</option>
<option value="Helm">Helm</option>
<option value="DirectAdmin">DirectAdmin</option>
<option value="Virtualmin">Virtualmin</option>
<option value="Usermin">Usermin</option>
<option value="Webmin">Webmin</option>
<option value="Hosting Director">Hosting Director</option>
<option value="Hsphere">Hsphere</option>
<option value="Dsm">Dsm</option>
<option value="Hosting Controller">Hosting Controller</option>
<option value="Company Control Panel">Company Control Panel</option>
</select>
</td>

And this is the php code i use at the end of my page

{literal}
<script>
selectItemInSelectBox=('control_panel', '{/literal}{$old.control_panel}{literal}');
</script>
{/literal}

So what i am trying to do is choose more than one options from the select box and enter them to database. But it only inserts one selection when i submit the form to the database.

If somebody helps i would really appreciate i tried a lot to find some information about this and eventually had to post it here.

Thanks in advance..

eelixduppy

10:12 pm on May 18, 2006 (gmt 0)



Change the following line of code to this:
<select size="5" name="control_panel[]" multiple tabindex="¦">

That way when the form is submitted its submitted in an array. Then you can do something like this:

$size = count($_POST['control_panel']);
$query = "insert into table_name (col1) values (\"";
for($i = 0; $i < $size; $i++) {
$query .= $_POST['control_panel'][$i]." "; //this all depends on how you want it stored
}
$query .= "\")";
mysql_query($query);

dreamcatcher

7:05 am on May 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Change the name as eelixduppy as mentioned. As an alternative to eelixduppy`s method, you can just implode the data. It depends on how you are storing it in your DB.

$data = implode(",", $_POST['control_panel']);

dc

blue_eagle

5:26 am on May 20, 2006 (gmt 0)

10+ Year Member



thanks a lot guys.