在更新页面下拉选择数据库中的当前数据

drop-down selecting current data from database at update page

当我在 add_content.php 选择类别时,我正在使用下面的代码并且它正在运行。

<select name="category"><?php

$q=mysql_query("select * from categories where sub='0' order by desc");
while ($maincategory=mysql_fetch_array($q)){

    echo "
    <option style=background-color:#BCE77C; value=$maincategory[id]>$maincategory[title]</b></option>";

$q1=mysql_query("select  * from categories where sub='".$maincategory['id']."' order by desc");
while ($subcategory=mysql_fetch_array($q1)){

    echo "<option value=$subcategory[id]>--$subcategory[title]</option>";
    }

}

?></select>

*

现在我正在研究 edit_content.php。 我想要从数据库中预先选择的当前类别。

我该怎么做;连接内容数据库以获取当前 ID,例如;

        // get the category the post is assigned to
        $query = mysql_query("SELECT category FROM article");
        while ($row = mysql_fetch_assoc($query)) {
            $chosenCategory = $row['id'];
        }

就像那样。但我不能更进一步。感谢您的帮助。

只是在回显时检查值是否等于所选值。

先获取id

 // get the category the post is assigned to
        $query = mysql_query("SELECT category FROM article");
        while ($row = mysql_fetch_assoc($query)) {
            $chosenCategory = $row['id'];
        }

然后使用option标签的selected属性

    <select name="category"><?php

    $q=mysql_query("select * from categories where sub='0' order by desc");
    while ($maincategory=mysql_fetch_array($q)){
    if($maincategory[id] == $chosenCategory)
{
        echo "<option style=background-color:#BCE77C; value=$maincategory[id] selected>$maincategory[title]</b></option>";
}
else
{
echo "<option style=background-color:#BCE77C; value=$maincategory[id]>$maincategory[title]</b></option>";

}

    }

    ?></select>