php设置下拉菜单和单选按钮的值以及从数据库中获取的数据


php setting value of drop down and radio button with data fetched from database

几天前开始在php中进行编码,现在面临以下问题。当php页面加载时,我从表中获取详细信息,并试图根据从表中获得的值设置下拉列表的值。

<select  title= "Fruit selected" name="fruit" id="fruit">
<option value="none selected">-Select Fruit-</option>
<?php if(!is_null($user)){ if($user['fruit']=="apple"){echo '<option value="apple" selected="selected">Apple</option>'}}else{echo '<option value="apple">Apple</option>'}?>
<?php if(!is_null($user)){ if($user['grad_semister']=="banana"){echo '<option value="banana" selected="selected">Banana</option>'}}else{echo '<option value="banana">Banana</option>'}?>
</select>

根据php,我认为我做的根本是愚蠢的,我不擅长基础知识。有人能告诉我们可能出了什么问题吗?到目前为止,当我使用上面的代码转到php页面时,我得到HTTP错误500

我想您错过了在每个回波之后。

应该是:

<?php if(!is_null($user)){ if($user['fruit']=="apple"){echo '<option value="apple" selected="selected">Apple</option>';}}else{echo '<option value="apple">Apple</option>';}?>
<?php if(!is_null($user)){ if($user['grad_semister']=="banana"){echo '<option value="banana" selected="selected">Banana</option>';}}else{echo '<option value="banana">Banana</option>';}?>

这可能不是php代码错误,也许服务器端的整个脚本或页面都有问题。

试着参考GEt方法和$user[],看看会发生什么。