Martin Ciok
Martin Ciok

Reputation: 21

Multiple select field only submitting last selected option

I've created a form where some of the fields are multiple selects, however after submitting the form only the final selection within the field where multiple selections were made is being inserted into mysql. I'm not sure what to do. I'm new to mysql and php so I need to keep it simple. Thank you in advance.

Here's my code:

if(isset($_POST['add'])){
    $dbhost = 'xxxxxxx';
    $dbuser = 'xxxxxxx';
    $dbpass = 'xxxxxxx';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn ){
        die('Could not connect: ' . mysql_error());
    }

    if(! get_magic_quotes_gpc() ){
        $TypeOfDwelling = addslashes ($_POST['TypeOfDwelling']);
        $EmailAdd = addslashes ($_POST['EmailAdd']);
    }else{
        $TypeOfDwelling = $_POST['TypeOfDwelling'];
        $EmailAdd = $_POST['EmailAdd'];
    }
    $sql = "
    INSERT INTO zzzzDB 
    ( TypeOfDwelling
    , EmailAdd
    , CreateDate) VALUES
    ('$TypeOfDwelling'
    ,'$EmailAdd'
    , NOW())
    ";
    mysql_select_db('xxxxx_xxxxx');
    $retval = mysql_query( $sql, $conn );
    if(! $retval ){
        die('Could not enter data: ' . mysql_error());
    }
    echo '<img src="../../../images/content__images/image_480x320_a.jpg" alt="image" width="300" height="200" class="img-responsive">'; 
    echo "<br />Thank you for your submission!";
    mysql_close($conn);
    ?>

    <form name="MLSrequest" action="<?php $_PHP_SELF ?>" method="post" class="form-inline" role="form">
        <table width="95%" cellspacing="0" cellpadding="0" border="0" align="left">
            <tbody>
                <tr>
                    <td>
                        <input type="hidden" name="recipient" value="[email protected]">          
                    </td>
                </tr>
                <tr>
                    <td colspan="2"> <div class="form-group">
                        <label class="sr-only" for="TypeOfDwelling">Select The Type Of Dwelling</label>
                        <select name="TypeOfDwelling" size="5" class="form-control form-control-lg" id="TypeOfDwelling" multiple="multiple">
                            <option selected>Select DWELLING TYPE.</option>
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                            <option value="4">4</option>
                         </select>
                     </div>
                     </td>
                 </tr>
                 <tr>
                     <td colspan="2">
                         <div class="form-group">
                             <label class="sr-only" for="EmailAdd">Email Address</label>
                             <input name="EmailAdd" type="email" class="form-control form-control-lg" id="EmailAdd" size="17px" maxlength="30" placeholder="Email Address" required>
                         </div>  
                     </td>
                 </tr>
                     <td colspan="2">
                         <button name="add" id="add" type="submit" class="btn btn-primary">Submit Request</button>          
                     </td>
                 </tr>
            </tbody>
        </table>
    </form>
<?php
}

Upvotes: 2

Views: 2705

Answers (1)

mickmackusa
mickmackusa

Reputation: 47971

Here is the trick, you need to set the select tag's name attribute value as an array like this:

<select name="TypeOfDwelling[]" multiple>

By doing this, you will give a "proper container" for all of the selected values to be stored in during submission.

This will give you an element in the $_POST array like this:

$_POST["TypeOfDwelling"]=array(....);

If you do not using the square brackets, then each subsequent value in TypeOfDwelling will overwrite the previous, as you noticed.


Let me explain it using a GET form situation...

Let's say this is your form:

<form action="">
  <select name="multi" multiple>
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
  </select>
  <button>Submit</button>
</form>

If you select options 2 and 4 and then submit, you would see a query string at the end of the url that says: ?multi=2&multi=4 If you wrote var_export($_GET["multi"]) you would see 4

If you instead use multi[] as your name attribute value, selected options 2 and 4, then submitted, you would see this query string: ?multi%5B%5D=2&multi%5B%5D=4 The %5B%5D is the url encoded version of []. If you wrote var_export($_GET["multi"]), you would see array(0=>'2',1=>'4')


Now, as far as your implementation, you will need to handle this array of data before you can just slap it into your query. You will need to decide whether you are going to insert multiple rows of data or perhaps comma-separate the values. If you are going to just comma-separate the values, you can use implode(',',$_POST["TypeOfDwelling"]) to reduce the array of values to a string. The right decision on this matter is out of the scope of this question, but you can surely find some good pages on SO on this topic with a little bit of searching.

The short answer is, you can implode them when you declare the variable for your query.

// in there respective locations...

$TypeOfDwelling = addslashes(implode(',',$_POST['TypeOfDwelling']));

// and

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

If I had more time, I'd give you more comprehensive help ,but for now, please immediately research prepared statements with mysqli -- this is necessary.

Upvotes: 3

Related Questions