如何从帖子编辑的下拉列表中获取所选值

I have the database for categories where i have id and name rows

<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>

for some reason this does not show which one was selected when trying to edit the submitted post

and i do retrieve the category_id like so:

$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'];
            }
    }   

I believe MySQL results are always returned as strings, so $category_id === 1 will be false.

Either try $category_id === "1" or $category_id == 1

Also, you need to echo the selected="selected outside the value attribute

I think the option argument for selected is

<option selected="selected"> </option>

You can also try like this as well.

<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>