我有一个下拉菜单,可以很好地编辑;
// Connect to Local Host
@ $db=mysqli_connect('localhost', 'root');
if (!$db) { do_Warning(-1); exit; }
// Connect to Database
@ mysqli_select_db($db,'coshh');
echo '<tr><td>Harmful effects:</td>
<td><select name="Harmful_effects"><option></option>';
/* Create SQL to extract effects options */
$sql = "SELECT * FROM `lookup_harmful_to_reproductive_system` ORDER BY `id`";
/* Perform SQL to get effects options */
$result = mysqli_query($db, $sql);
/* Build dropdown list for each effects option found in result */
while ($row=mysqli_fetch_array($result,MYSQLI_BOTH))
{
echo '<OPTION value="'.$row['Harmful_effects'].'"';
if ($Harmful_effects == $row['Harmful_effects'])
{
echo ' selected';
}
echo '>'.$row['Harmful_effects'].'</OPTION>';
}
echo '</select></select></td>';
然而,我被要求将其更改为多选项下拉菜单,我已经这样做了,它将多个选项插入到我的MySQL数据库中。我遇到的一个大问题是,当你编辑这个多下拉菜单时,它不会突出显示以前从数据库中选择/输入的数据。我已经有了一个开始,但我真的很难理解;
echo '<tr><td>Harmful effects:</td>
<td><select name="Harmful_effects[]" multiple="multiple" STYLE="width: 500px" size="0"><option></option>';
/* Create SQL to extract effects options */
$sql = "SELECT * FROM `lookup_harmful_to_reproductive_system` ORDER BY `id`";
/* Perform SQL to get effects options */
$result = mysqli_query($db, $sql);
/* Build dropdown list for each effects option found in result */
while ($row=mysqli_fetch_array($result,MYSQLI_BOTH))
{
echo '<OPTION value="'.$row['Harmful_effects'].'"';
if ($Harmful_effects == $row['Harmful_effects'])
{
echo ' selected';
} echo '>'.$row['Harmful_effects'].'</OPTION>';
} echo '</select></select></td>';
如有任何建议/帮助,我们将不胜感激!
我想您在$Harmful_effects
和$row['Harmful_effects']
方面遇到了一些问题。也许它应该更像:
if (in_array($row['harmful_effects'], $harmful_effects) {
echo 'selected';
}
这取决于如何将选定效果的数组存储到数据库中,以及如何初始化变量$harmful_effects
。
$Harmful_effects
应该是一个数组,包含选择控件的保存结果。然后您可以使用以下测试来完成多选。
if (in_array($row['Harmful_effects'], $Harmful_effects)) {echo ' selected';}