做这个
//Suppose the $selecteValue contains the selected value which you have fetched from the database
$selectedValue = 'test';
$sql = "SELECT Program_Description FROM programs";
$result = mysqli_query($dbc, $sql);
$dropdown = "<select name='Program_Description'>";
$dropdown .= "<option value= ></option>";
while($row = mysqli_fetch_assoc($result)) {
$dropdown .= "\r\n<option ".(($selectedValue == $row['Program_Description']) ? ' selected ' : '')." value='{$row['Program_Description']}'>{$row['Program_Description']} </option>";
}
$dropdowna .= "\r\n</select>";
另一种解决方案是
$sql = "SELECT Program_Description FROM programs";
$result = mysqli_query($dbc, $sql);
$dropdown = "<select name='Program_Description'>";
$dropdown .= "<option value= ></option>";
$strSelect = '';
while($row = mysqli_fetch_assoc($result)) {
if ($selectedValue == $row['Program_Description']) { $strSelect = ' selected '; } else { $strSelect = ''; }
$dropdown .= "\r\n<option ".$strSelect." value='{$row['Program_Description']}'>{$row['Program_Description']} </option>";
}
$dropdowna .= "\r\n</select>";