Iain Simpson
Iain Simpson

Reputation: 8121

Posted multiselect box selection to database only adding the first selected item

i am using the following multiselect box to take input from users, this then gets posted to my php form which adds it to the database, the problem is, all I am getting added is the first selection, if the user selects more than one field I still only get the first field.

If the user selects lets say internet, drawing,maths I want that to be put into the database, at the moment all that is inserted is internet, or whatever is the first thing selected in the list.

My form looks like this >>

    <form action="../files/addtodb.php" method="post" style="width:800px;">
    <select name="whatisdeviceusedfor[]" size="1" multiple="multiple" id="whatisdeviceusedfor[]">
        <option value="games">Games</option>
        <option value="takingphotos">Taking Photos</option>
        <option value="maths">Maths</option>
        <option value="reading">Reading</option>
        <option value="drawing">Drawing</option>
        <option value="internet">Internet</option>
        <option value="other">Other (enter more info below)</option>
      </select>
      <input type="submit" name="submit" id="submit" value="Submit">
      </form>

The php side looks like this >>

 <?php

 // Implode what is device used for
$usedfor = $_POST['whatisdeviceusedfor'];
$arr = array($usedfor);
$whatisdeviceusedfor = implode(" ",$arr);



// Insert posted data into database
mysql_query("INSERT INTO itsnb_year5questionaire (whatisdeviceusedfor) VALUES   '$whatisdeviceusedfor'") or die(mysql_error()); 

   ?>

Upvotes: 0

Views: 2007

Answers (4)

Toretto
Toretto

Reputation: 4711

Because $usedfor is contain the array you can directly use it in foreach to save the each value in the value.Try this it may help you:

<?php   
$usedfor = $_POST['whatisdeviceusedfor'];

foreach($usedfor as $arr){
mysql_query('INSERT INTO itsnb_year5questionaire(whatisdeviceusedfor) VALUES("'.$arr.'"') or die(mysql_error()); 

}

Upvotes: 1

NullPoiиteя
NullPoiиteя

Reputation: 57322

you are already getting array by select so you dont need to use $arr = array($usedfor);this again

just try

$usedfor = $_POST['whatisdeviceusedfor'];

$whatisdeviceusedfor = implode(" ",$usedfor);

or

$usedfor = $_POST['whatisdeviceusedfor'];


$strings ='';
foreach ($usedfor as $item){
    $strings .=' '. $item;
}
echo $strings;

and

 <select name="whatisdeviceusedfor[]" size="5" multiple="multiple" id="whatisdeviceusedfor[]">
                                           ^^             
                                           ||change to option you want to select 

i have changed size="5" so it will select now 5 at a time ...you have only 1 so it will let only 1 select at a time

result

enter image description here

warning

your code is vulnerable to SQL injection also use of mysql_* function are deprecated use either PDO or MySQLi

Upvotes: 2

echo_Me
echo_Me

Reputation: 37253

first change size = "5" to see good the list

then try to make like that (its second option if you like it)

if ($_POST) {

  // post data

 $games = $_POST["games"];
 $takingphotos = $_POST["takingphotos"];
 .
  ...


  // secure data

  $games = uc($games);
  $boats = uc($takingphotos);
  .
  ...
 }   


  // insert data into database

  $query = "INSERT INTO itsnb_year5questionaire (games, takingphotos,....)   
             VALUES('$games','$takingphotos',......)";
     mysql_query($query) or die(mysql_error());

    echo 'Thanks for your select!';

Upvotes: 1

feeela
feeela

Reputation: 29932

If you have selected mutliple items via a SELECT or via CHECKBOXES, the form will return an array-like data structure. You will always need to loop over that data and store each item individually into the database or concatenate the values to a string before inserting.

Upvotes: 1

Related Questions