0

当我再次登录时,是否可以在从 mysql 到 php 的下拉列表中显示保存的值?我的代码将值保存在 Mysql 中。但是当我重新登录时,它显示空白字段(这是下拉列表中的默认值)

$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 value='{$row['Program_Description']}'>{$row ['Program_Description']}     </option>";
}
$dropdowna .= "\r\n</select>";
4

2 回答 2

1

尝试这个,

    $sql = "SELECT Program_Description FROM programs";
$result = mysqli_query($dbc, $sql);
$savedValueInDb = ""; //fetch the DB saved value and assign to this variable
$dropdown = "<select name='Program_Description'>";
$dropdown .= "<option value= ></option>";
while($row = mysqli_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['Program_Description']}'";
$dropdown .= ($savedValueInDb == $row ['Program_Description']) ? " selected " : "";
$dropdown .= ">{$row ['Program_Description']}     </option>";
}
$dropdowna .= "\r\n</select>";
于 2013-09-18T10:29:07.127 回答
1

做这个

 //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>";
于 2013-09-18T10:44:21.900 回答