我有以下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则必须输入我的MySQL表的s2
列等等。
答案 0 :(得分: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."')";
答案 1 :(得分: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
}