HTML从数据库中选择默认值

时间:2015-05-12 05:13:06

标签: php html

我正在尝试添加HTML" select dropdown list"性别。但是,我希望使用php框架从数据库中获取默认值。我能这样做吗?

2 个答案:

答案 0 :(得分:0)

伪代码:

$default_gender = getDefaultGenderValueFromDB;
<select name="genderSelected">
<option value="m"<?php echo ($default_gender == 'm')? selected:'';?>>Male</option>
<option value="f"<?php echo ($default_gender== 'f')? selected:'';?>>Female</option>
</select>

有效的代码段:

<?php $default_gender = 'm'; ?>

<select name="genderSelected">
<option value="m" <?php echo ($default_gender == 'm')? 'selected="selected"':'';?>>Male</option>
<option value="f" <?php echo ($default_gender == 'f')? 'selected="selected"':'';?>>Female</option>
</select>

答案 1 :(得分:0)

试试这个:

<td>Sex: </td> <td>  <select name="Sex">
<?php
    $result = mysqli_query($connection, "Select sex from personaltab where empno='$emplno'");
    $rows = mysqli_fetch_assoc($result);
     $gender =$rows['sex'];
        echo "<option if($gender == 'FEMALE') { selected='selected'} value='FEMALE'>FEMALE</option>";
        echo "<option value='MALE' if($gender == 'MALE') { selected='selected' }>MALE</option>";




    ?>
   </select> </td></tr>

不要使用if else ..