无法从数据库 mysql 获取保存的值作为下拉列表中的选定值.我尝试更新用户已经填写的表格


Not able to get saved value from db mysql as selected value in dropdown. I trying to update the form which user has already filled

>我正在尝试从数据库中获取数据,所有值都反映了良好,但未选择下拉列表的保存值。

<div class="col-md-4">
                        <select name="OfficeName" id="OfficeName" class="form-control"  onchange="getText(this)" required>
            <?php 
            while($data = dbFetchAssoc($result)){
            ?>
            <option value=''><?php echo $data['off_name']; ?></option>
            <?php 
            }//while
            ?>
            </select>   
 </div>

这是一个常见问题。

假设您有一个 select 语句,其中填充了数据库中的键值对:

<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option value="<?= $value ?>"><?= htmlentities($label) ?></option>
<?php endforeach ?>

提交表单后,您将获得 _GET 美元或 _POST 美元(或 _REQUEST 美元)的 sel 值。

<?php
$selected = $_POST['sel'];
?>

要选择相应的选项,请更新上面的代码,如下所示:

<select name="sel">
<?php foreach ($options as $value => $label) : ?>
<option <?php if ($selected == $value) : ?>selected<?php endif ?> value="<?= $value ?>">
<?= htmlentities($label) ?>
</option>
<?php endforeach ?>