简体   繁体   中英

How to store multiple selected values from <select> tag in a single field

I have a select tag with multiple="multiple" . And a user can select more than one value.

<label for="aoi">Area of Interest:</label>
<select id="sel_aoi" name="aoi" multiple="multiple">
    <option value="hr-executive">HR Executives</option>
    <option value="sr-manager">Sr. Manager</option>
    <option value="service-advisor">Service Advisor</option>
    <option value="production">Production Engineer</option>
    <option value="mechanical">Mechanical Engineer</option>
</select>

How I can store these more than one selected values in single field of MySQL database?

Store it to an array and save the values to database using comma seprated.

<label for="aoi">Area of Interest:</label>
          <select id="sel_aoi" name="aoi[]" multiple="multiple">
            <option value="hr-executive">HR Executives</option>
            <option value="sr-manager">Sr. Manager</option>
            <option value="service-advisor">Service Advisor</option>
            <option value="production">Production Engineer</option>
            <option value="mechanical">Mechanical Engineer</option>
          </select>

process.php

$aoi = implode(',', $_POST['aoi']);

When you get your values from your $_POST insert them in with a mysql insert multiple values like this:

insert into myTable 
    (someColumn, someOtherColumn) 
values 
    (someValue1, someOtherValue1), 
    (someValue2, someOtherValue2), 
    (someValue3, someOtherValue3)

Edit: If you want the in one row use something like PHP implode ():

$qry="insert into mytable (someColumn) values (".implode(',',$myArray).")

Not that you want to make sure that the $myArray variable is clean of anything that could SQJ Injection attack you database. A prepared statement would be a suggestion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM