cdr6800
cdr6800

Reputation: 105

All values selected in mutiple selection box not recording in MySQL database

On this page, http://www.pfacmeeting.org/2016/phpabstracts/submit_form.php, there is a multiple selection box with 4 choices. Here is the code for that element:

<select id="audience" name="audience" multiple="multiple">
  <option value="Newly licensed or unlicensed fiduciaries">Newly
   licensed or unlicensed fiduciaries</option>
  <option value="Experienced fiduciaries">Experienced
   fiduciaries</option>
  <option value="Attorneys">Attorneys</option>
  <option value="Other">Other</option>
</select>

I did a test a couple of times and selected Newly licensed or unlicensed fiduciaries and Attorneys. However, in the DB field, it only recorded Attorneys. In the php file that processes the form (submit_process.php), first I capture the field to variable:

$audience = mysql_escape_string($_POST['audience']);

Then I insert it into the table:

$query = "INSERT INTO abstracts VALUES ('', ......'$audience','.......)";

The dots just represent other variables before and after audience. The data is recording into the field, but as I said, it is just the second choice - Attorneys. Any idea why the first choice is not recording also? Any help would be much appreciated. Thank you.

Upvotes: 1

Views: 51

Answers (2)

user557846
user557846

Reputation:

multi select produces an array.

To insert all selected in one field implode the array

$audience = mysql_escape_string(implode(',',$_POST['audience']));

oh as KAD says name="audience[]" is required to.

side note- mysql_* depreciated start moving away from it as soon as you can.

Upvotes: 1

KAD
KAD

Reputation: 11122

The multi-select post a PHP associative array so you will need to loop through the selected results of the array and insert them for each line.

    foreach($_POST['audience'] as $audience)
    {
      $query = "INSERT INTO abstracts VALUES ('...." 
              . mysql_escape_string($audience) . ",.......)";

    }

also, you need to add the array symbol in the name :

<select id="audience" name="audience[]" multiple="multiple">

Upvotes: 1

Related Questions