我有一个表单,用户可以在其中输入部门名称,然后是多个选择框,其中包含在该部门中工作的多个员工职位/职位。然后通过foreach()循环将记录保存到我的SQL表中,这样就可以得到结果:
我现在正在寻找一种方法来允许用户编辑他们提交的部门和位置,因此想要在一个字段中再次显示存储的记录,这使我无法将数组数据恢复到选择字段。 ..
我试图使用另一个foreach来填充<select>
字段但到目前为止没有运气。我不确定问题是否只是错误的代码或者Select2插件是否也给我带来了问题......
PHP(update2)
<?php
// Start MySQLi connection
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
// Build basic query, Admins can see all records
$sql = ("SELECT * FROM qci_departments GROUP BY Department ORDER BY Department");
// run the query or show an error message
if(!$result = $db->query($sql)){
echo('There was an error selecting data from the table [' . $db->error . ']');
}
while($row = mysqli_fetch_array($result)){
$dept_id = $row['ID'];
$dept_name = $row['Department'];
$dept_positions = $row['Positions'];
echo "
<tr>
<td>
<input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\">
</td>
<td>$dept_name</td>
<td>
<select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control\" multiple>
<option value='".$dept_positions."'>".$dept_positions."</option>";
//$position_query = ("SELECT distinct Positions FROM qci_departments");
$position_query = ("SELECT distinct Positions FROM qci_departments where Department = '".$row['Department']."'");
if(!$result_positions = $db->query($position_query)){
echo('There was an error selecting data from the table [' . $db->error . ']');
} else {
while($row_positions = mysqli_fetch_assoc($result_positions)){
echo "<option value='".$row_positions['Positions']."'>".$row_positions['Positions']."</option>";
}
}
echo "</select>
</td>
</tr>";
有人能指出我正确的方向吗? 谢谢, A2K
编辑:更新代码以反映下面海报建议的更改
答案 0 :(得分:1)
我会给你两个解决方案
首先:
while($row = mysqli_fetch_array($result)){
$dept_id = $row['ID'];
$dept_name = $row['Department'];
$dept_positions = $row['Positions'];
echo "
<tr>
<td>
<input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\">
</td>
<td>$dept_name</td>
<td>
<select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control select2\">
<option value='".$dept_positions."'>".$dept_positions."</option>";
$position_query = ("SELECT distinct Positions FROM qci_departments");
if(!$result_positions = $db->query($position_query)){
echo('There was an error selecting data from the table [' . $db->error . ']');
} else {
while($row_positions = mysqli_fetch_assoc($result_positions)){
echo "<option value='".$row_positions['Positions']."'>".$row_positions['Positions']."</option>";
}
}
echo "</select>
</td>
</tr>";
}
在while内进行第二次查询以获取位置并填充选择选项。
第二://我更喜欢这个
$positions_arr = array();
$position_query = ("SELECT distinct Positions FROM qci_departments");
if(!$result_positions = $db->query($position_query)){
echo('There was an error selecting data from the table [' . $db->error . ']');
} else {
while($row_positions = mysqli_fetch_assoc($result_positions)){
$positions_arr[] = $row_positions['Positions'];
}
}
while($row = mysqli_fetch_array($result)){
$dept_id = $row['ID'];
$dept_name = $row['Department'];
$dept_positions = $row['Positions'];
echo "
<tr>
<td>
<input type=\"text\" class=\"form-control\" id=\"editDeptName\" name=\"editDeptName\" value=\"$dept_id\">
</td>
<td>$dept_name</td>
<td>
<select id=\"editDeptPositions\" name=\"editDeptPositions\" class=\"form-control select2\">
<option value='".$dept_positions."'>".$dept_positions."</option>";
foreach($positions_arr as $value){
echo "<option value='".$value."'>".$value."</option>";
}
echo "</select>
</td>
</tr>";
}
制作一个包含所有位置的数组,然后在其中制作一个foreach以填充选择选项