Page 1 of 1

error..insert multiple value in "Select Option" in table?

Posted: Wed Nov 09, 2011 12:26 am
by pradeepcarya
Hi,
I created "scrollble Multi Select Option" as per below code.

<form name=myform>
<select name=mytextarea size=3 multiple>
<option name=one value=one> one </option>
<option name=two value=two> two </option>
<option name=three value=three> three </option>
<option name=four value=four> four </option>
</select>
</form>
---------------------------

but when i am inserting this value via php page into mysql table "tab1",only last selected value is getting inserted.

insert into tab(col1)
values($_POST['mytextarea ']);


plz let me know where i m wrong....thnx in advance

Re: error..insert multiple value in "Select Option" in table

Posted: Wed Nov 09, 2011 4:17 am
by twinedev
For use in PHP, to have an array of all selected items, you need to include a set of [] after the name so that PHP will put the items in an array (same for checkboxes)
(and also, the <option> tag does not support the name attribute, page won't validate)

Code: Select all

<form name="myform" method="post" action="#">
    <select name="mytextarea[]" size="3" multiple="multiple">
        <option value="one"> one </option>
        <option value="two"> two </option>
        <option value="three"> three </option>
        <option value="four"> four </option>
    </select>
</form> 
Then in your code, $_POST['mytextarea'] will be an array of the items selected. You will have to code how to put this in your database (ie, is each one a separate row, or they getting entered all in the same field (ex. $strMyTextArea = implode(',',$_POST['mytextarea']); )

-Greg