0

When someone on my website chooses regular service it must also choose the days it will execute. Therefore, I use this code to choose one or more weekdays:

<select class="form-control select2" name="tyzden[]" multiple="multiple" data-placeholder="Zvoľte deň" style="width: 100%;">        
  <option value="Cely">Celý týždeň</option>
  <option value="Pondelok">Pondelok</option>
  <option value="Utorok">Utorok</option>
  <option value="Streda">Streda</option>
  <option value="Štvrtok">Štvrtok</option>
  <option value="Piatok">Piatok</option>
  <option value="Sobota">Sobota</option>
  <option value="Nedeľa">Nedeľa</option>
</select>

<input style="width:97.3%; margin-left:15px;" type="submit" name="pridat" value="Pridať odvoz">

And this is a PHP code:

if(isset($_POST['pridat'])) {
  $tyzden = $_POST['tyzden'];
  foreach ($_POST['tyzden'] as $a) {
    $zapis = mysql_query("INSERT INTO odvozy SET tyzden='$a';");
  }
}

On phpMyAdmin I see only the last option selected. Can you assist?

aynber
  • 22,380
  • 8
  • 50
  • 63
ludvik120
  • 49
  • 7
  • Welcome to SO. Please vist the [help] to see what and how to ask. – mplungjan Jan 23 '16 at 18:05
  • Possible duplicate of [insert multiple rows via a php array into mysql](http://stackoverflow.com/questions/779986/insert-multiple-rows-via-a-php-array-into-mysql) – mplungjan Jan 23 '16 at 18:13
  • @trincot Looks like I copied the wrong link. – grepsedawk Jan 23 '16 at 18:22
  • 1
    Possible duplicate of: [How to get multiple selected values of select box in php?](http://stackoverflow.com/questions/2407284/how-to-get-multiple-selected-values-of-select-box-in-php) – grepsedawk Jan 23 '16 at 18:22

1 Answers1

0

It appears you specify name='tyzden[]' as an array entry in your html. This would actually mean that you're having an array formatted like this:

$_POST['pridat'] = array(
    "tyzden" => array(
        "0" => array([selected inputs here])
    )
);

By making the name of the html select simply name='tyzden', you will get an array formatted like this:

$_POST['pridat'] = array(
    "tyzden" => array(
        [selected inputs here]
    )
);

Which will therefore make your php code work as expected.

grepsedawk
  • 3,324
  • 2
  • 26
  • 49