-1

I have added a form Select where a person can choose which award their dog has. The problem is it only inserts one option into the database and it is to be a multiple select. Allowing people to add all option in the select box.

$query  = "UPDATE dogs SET ";
    $query .="dog_name = '{$dog_name}', ";
    $query .="affix = '{$affix}', ";
    $query .="owners = '{$owners}', ";
    $query .="sire = '{$sire}', ";
    $query .="dam = '{$dam}', ";
    $query .="dob = '{$dob}', ";
    $query .="sex = '{$sex}', ";
    $query .="award = '{$award}', ";
    $query .="champ = '{$champ}', ";
    $query .= "dog_photo = '{$dog_photo}' ";
    $query .= "WHERE dog_id = {$the_dog_id} ";

    $update_dog = mysqli_query($connection,$query);


<select class="form-control" name="award" multiple="multiple" data-plugin-multiselect data-plugin-options='{ "maxHeight": 200, "enableCaseInsensitiveFiltering": true }' id="ms_example6">
     <option value="JW">JW</option>
     <option value="BOY">Bulldog Of Year</option>
     <option value="BOY">Crufts Champion</option>
     <option value="JC">Junior Champion</option>
        </select>
garyfalkland
  • 19
  • 1
  • 8
  • Your code is vulnerable to SQL injection. You should use prepared statements. – Dharman Oct 29 '19 at 08:44
  • Firstly you should view your Post Data using something like var_dump($_POST). Secondly, your database design is not well thought out. We know you want "one of many" entries for awards but then you have owners ( more than 1?) etc. Do you know how you are going to handle those? And I am guessing your duplicated Option value of BOY with different names is just a typo which will also give you grief. – TimBrownlaw Oct 29 '19 at 10:16
  • Yes the BOY is a typo. I agree the database design is messy and not well thought out. It is for a dog show results website and the idea is to show a dogs info and part of that will display images based on what awards they achieved. – garyfalkland Oct 29 '19 at 16:34

1 Answers1

0

Just loop through it.

Assuming your form is a POST request, name the field as an array

name="award[]"

and then iterate through each entry.

foreach($_POST['award'] as $award) {
  $query  = "UPDATE dogs SET ";
  $query .="dog_name = '{$dog_name}', ";
  $query .="affix = '{$affix}', ";
  $query .="owners = '{$owners}', ";
  $query .="sire = '{$sire}', ";
  $query .="dam = '{$dam}', ";
  $query .="dob = '{$dob}', ";
  $query .="sex = '{$sex}', ";
  $query .="award = '{$award}', ";
  $query .="champ = '{$champ}', ";
  $query .= "dog_photo = '{$dog_photo}' ";
  $query .= "WHERE dog_id = {$the_dog_id} ";

  $update_dog = mysqli_query($connection,$query);
}
Markus Zeller
  • 8,516
  • 2
  • 29
  • 35
  • What's wrong, why downvote? – Markus Zeller Oct 29 '19 at 08:57
  • It's close. Your first answer does the trick of providing all the selections. The OP needs to know how to view the $_POST using var_dump() or something to See whats happening. I think the problem lays in the creating a whole new duplicated entry for each award. Kind of nearly needs an awards table to list the actual awards and a dogs_awards join table to associate a dog with it's One to Many possible awards. – TimBrownlaw Oct 29 '19 at 10:08
  • Your correct Tim had another look at this and I need to start with better database design. So I am going to set up a table of awards and then do an inner join to the dog table. – garyfalkland Nov 03 '19 at 07:01