簡體   English   中英

在php和mysql中重新打開頁面時,在下拉列表中顯示已保存的數據

[英]display saved data in dropdown list when reopen the page in php and mysql

當我再次登錄時,是否可以在從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>";

嘗試這個,

    $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>";

做這個

 //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>";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM