我有类别的数据库,其中我有id和名称行
<select name="category_id">
<option value="<?php if($category_id === 1) { echo 'selected';} ?>">Electronics</option>
<option value="<?php if($category_id === 2) { echo 'selected';} ?>">Automotive</option>
</select>
出于某种原因,这并未显示在尝试编辑提交的帖子时选择了哪一个
我确实像这样检索category_id:
$res6 = mysql_query("SELECT * FROM `posts` WHERE `id` = '" . $id . "' LIMIT 1");
if(mysql_num_rows($res6) > 0){
while($row6 = mysql_fetch_assoc($res6)){
$id = $row6['id'];
$category_id = $row6['category_id'];
$price = $row6['price'];
$cover = $row6['cover'];
$title = $row6['title'];
$description = $row6['description'];
}
}
答案 0 :(得分:4)
我认为所选的选项参数是
<option selected="selected"> </option>
答案 1 :(得分:1)
我相信MySQL结果总是以字符串形式返回,因此$category_id === 1
将为false。
尝试$category_id === "1"
或$category_id == 1
此外,您需要回显selected="selected
属性
value
答案 2 :(得分:0)
您也可以尝试这样做。
<select name="category_id">
<?php
$cats = array("1"=>"Electronics", "2"=>"Automotive");
foreach($cats as $k=>$cat){
if( $k == $category_id )
echo "<option value='{$k}' selected='selected'>{$cat}</options>";
else
echo "<option value='{$k}' >{$cat}</options>";
}
?>
</select>