-1

I want to select multiple choice and insert the values in database. How can I do this?

     <select name="instructor" multiple>
      <option value="9000">Zaher</option>
      <option value="9001">Samih</option>
      <option value="9010">Majd</option>
     <select>

    $instructor=$_GET['instructor'];
    if(isset($_SESSION['courses']) && isset($_SESSION['semester'])){

       $coursechosen= $_SESSION['courses'];
        $semesterchosen=$_SESSION['semester'];
    $query="Insert INTO coordinators(instructor_id,course_code,semester) 
    VALUES ('$instructor','$coursechosen','$semesterchosen')";

    mysql_query($query);
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
aaaz
  • 3
  • 1
  • 1
  • 5
  • Please check the link:- http://bytes.com/topic/php/answers/956318-how-insert-multiple-values-into-mysql-multiple-select-option-form – Alive to die - Anant Apr 04 '15 at 11:58
  • You have a SQL injection opportunity in `$instructor` - don't go live without fixing this! It is recommended to switch to a newer database library, so you can use parameter binding, which will help avoid this problem. – halfer Apr 04 '15 at 12:15
  • @halfer sorry but I didn't understand – aaaz Apr 04 '15 at 12:59
  • [Read this](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – halfer Apr 04 '15 at 13:15

4 Answers4

4
<select name="instructor[]" multiple>
      <option value="9000">Zaher</option>
      <option value="9001">Samih</option>
      <option value="9010">Majd</option>
     <select>

// Your PHP code
<?php
foreach ($_GET['instructor'] as $value) {
            $instructor.= $value.", ";
        }

    if(isset($_SESSION['courses']) && isset($_SESSION['semester'])){

       $coursechosen= $_SESSION['courses'];
        $semesterchosen=$_SESSION['semester'];
    $query="Insert INTO coordinators(instructor_id,course_code,semester) 
    VALUES ('$instructor','$coursechosen','$semesterchosen')";

    mysql_query($query);
    }
?>
Micessien
  • 431
  • 1
  • 4
  • 7
2
<select name="instructor[]" multiple>
<?php 
foreach ($_POST['instructor'] as $icon) 
{
 ///your insert code//


}?>
Vivek Singh
  • 2,453
  • 1
  • 14
  • 27
0
        <select name="instructor[]" multiple>
              <option value="9000">Zaher</option>
              <option value="9001">Samih</option>
              <option value="9010">Majd</option>
             <select>
                <input type="submit" value="send" name="send">
         
       //php code
        <?php
        if (isset($_POST['send']))
    {
        if (isset($_POST['instructor']))
        {
            $instructor= implode(',',$_POST['instructor']);
            $query="INSERT INTO `database` VALUES ('0','$instructor')";

mysql_query($query);
        }
        else
        {
            print "no selected";
        }
    }
        
        ?>
  • 1
    Code-only answers like this are often down-voted here. Please [edit](https://stackoverflow.com/posts/65099721/edit) your answer and explain what you changed or how it works. – Arthur Pereira Dec 05 '20 at 00:19
  • implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. https://www.php.net/manual/en/function.implode.php – Mojtaba Rahmandoost Dec 06 '20 at 18:21
0
First use instructor as instructor[] so will become an array

<select name="instructor[]" multiple>
  <option value="9000">Zaher</option>
  <option value="9001">Samih</option>
  <option value="9010">Majd</option>
 <select>

In php file 
$instructor = $_POST['instructor'];

USE THE IMPLODE FUNCTION OF PHP
$instructor= implode(",",$instructor);

so result will be 9000,9001,9010
NOW $instructor = 9000,9001,9010;

$sql = "INSERT INTO `TABLE`(`instructor`) VALUES ('{$instructor }')";