2017-10-12 67 views
0

插入表中的多个列我有以下的HTML代码:如何从下拉字段

<form method="POST" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> 
Faculty ID:<input type="text" name="fid" id="fid" value=""> 
<span class="error">*</span><br><br> 
Sunday: <select name="sun[]" id="sunday" multiple="multiple"> 
    <option value="11">8:15-9:15</option> 
    <option value="12">9:15-10:15</option> 
    <option value="13">10:15-11:15</option> 
    <option value="14">11:15-12:15</option> 
    <option value="15">12:15-1:15</option> 
    <option value="16">1:15-2:15</option> 
    <option value="17">2:15-3:15</option> 
    <option value="18">3:15-4:15</option> 
    <option value="19">4:15-5:15</option> 
    </select> 
    <span class="error">*</span><br><br> 
<button type="submit" value="Submit">Submit</button> 
</form> 

我如何可以插入从下拉列表(星期日)为不同的列中选择多个值吗?

例如,如果8:15-9:15被选中,那么其值必须输入到s1列到表中,如果9:15-10:15,然后它必须被输入到的s2柱我的MySQL表等等。

回答

0

入门所选变量从下拉列表

$sunday = array('-select time--', '8:15-9:15', '9:15-10:15' and so on); 
$selected_key = $_POST['sun']; 
$selected_val = $sun[$_POST['sun']]; 

将数据插入到不同的表

$query = "INSERT INTO `" . $selected_val . "` 
VALUES ('".$x"','".$y."','".$z."')"; 
0

可以存储的下拉选项到一个数组,其值的键是列名。然后你可以检查选择了哪些选项,并将相应的列添加到另一个数组中。然后你插入数据:

$columns = array(
     "8:15-9:15" => s1, 
     "9:15-10:15" => s2, 
     "10:15-11:15" => s3, 
     "11:15-12:15" => s4, 
     "12:15-1:15" => s5, 
     "1:15-2:15" => s6, 
     "2:15-3:15" => s7, 
     "3:15-4:15" => s8, 
     "4:15-5:15" => s9 
    ); //create a map between the options and column names 

    //retrieve the selected options to an array 
    $selectedOptions = $_POST['sun']; 

    $selectedColumns = array(); 

    if(count($selectedOptions)) { 
    //add the column name for each selected option to an array 
    foreach ($selectedOptions as $option) { 
     $selectedColumns[] = $columns[$option]; 
    } 
    // create a string with the columns names 
    $columnsToInsert = implode(", ", $selectedColumns); 
    // create a string with the selected options 
    $valuesToInsert = implode("', '", $selectedOptions); 
    // create the query string 
    $query = "INSERT INTO your_table('$columnsToInsert') VALUES ('" . $valuesToInsert ."')"; 
    // then you execute the query 
    } 
相关问题