如何将多个选择值插入数据库?

时间:2015-04-04 11:53:26

标签: php html

我想选择多项选择并在数据库中插入值。我怎么能这样做?

     <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);
    }

3 个答案:

答案 0 :(得分:1)

<select name="instructor[]" multiple>
<?php 
foreach ($_POST['instructor'] as $icon) 
{
 ///your insert code//


}?>

答案 1 :(得分:1)

<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);
    }
?>

答案 2 :(得分:0)

Sub NaturalSort(arr)
'Purpose: natural sort of 2nd and 3rd token (numbers in brackets)
'Note:    assumes "vertical" 2-dim array holding criteria in 2nd column
    Dim cnt As Long, nxt As Long, temp, temp2
    For cnt = LBound(arr) To UBound(arr) - 1
        For nxt = cnt + 1 To UBound(arr)
            If arr(cnt, 2) > arr(nxt, 2) Then
                temp = arr(cnt, 1):        temp2 = arr(cnt, 2)
                arr(cnt, 1) = arr(nxt, 1): arr(cnt, 2) = arr(nxt, 2)
                arr(nxt, 1) = temp:        arr(nxt, 2) = temp2
            End If
        Next nxt
    Next cnt
End Sub