1

我创建了一个“插入产品”页面,其中有一些与产品相关的字段和一个下拉框,其中显示了与特定产品相关的“类别”和“子类别”。

当我更新任何特定产品时,它会打开产品页面并在表单字段中从数据库中获取数据,但在下拉列表中它会显示第一个给定名称。我希望它选择产品所属的选定类别。

现在它显示数据库中的第一个项目。

        $select_query=  "select * from products LEFT JOIN product_description 
        ON products.product_id='".$id."' and 
        product_description.product_id='".$id."' 
        where products.product_id='".$id."'     ";

        $sub_category_id2 = $fetch_select['sub_category_id'];




        echo "<tr><td>Select Category </td><td><select name='category'>";

        $select_query=          'Select * from category';

        $select_query_run =     mysql_query($select_query);

        $sub_category_query=   "Select * from sub_categories 
                     where category_id='".$select_query_array['category_id']."'";


        while ($select_query_array=   mysql_fetch_array($select_query_run) ) 
        {
         echo

         "<optgroup label='".$select_query_array['name']."' >".
        $sub_category_query="Select * from sub_categories 
        where category_id='".$select_query_array['category_id']."'";


        $sub_query_run=         mysql_query($sub_category_query);

        while   ($sub_query_run_fetch=   mysql_fetch_array($sub_query_run) ) 
         {

            echo    'sub1 '.$sub_category_id =  
                        $sub_query_run_fetch['sub_category_id'] ;

                echo 
                "<option value=\"$sub_category_id\"";

                    if($sub_category_id==$sub_category_id2)

                echo ' selected="selected"';

                echo " >" .
                htmlspecialchars($sub_query_run_fetch['sub_category_name']) . "</option>";
                 }

                    echo "</optgroup>";
             }

在此处输入图像描述

                <tr><td>Select Category </td><td>


<select name='category'><optgroup label='Rings' >Select * from sub_categories 
                            where category_id='1'sub1 1<option value="1" >Gold</option>sub1 2<option value="2" >Silver</option>sub1 3<option value="3" >Diamond</option></optgroup><optgroup label='Bracelets' >Select * from sub_categories 
                            where category_id='2'sub1 4<option value="4" >Diamond</option>sub1 6<option value="6" >Gold</option>sub1 7<option value="7" >Silver</option></optgroup><optgroup label='Necklaces' >Select * from sub_categories 
                            where category_id='3'sub1 10<option value="10" >Diamond</option>sub1 11<option value="11" >Gold</option>sub1 12<option value="12" >Silver</option></optgroup><optgroup label='Earrings' >Select * from sub_categories 
                            where category_id='4'sub1 16<option value="16" >Diamonds</option>sub1 17<option value="17" >Gold</option>sub1 18<option value="18" >Silver</option></optgroup><optgroup label='Chains' >Select * from sub_categories 
                            where category_id='5'sub1 27<option value="27" >Diamond</option>sub1 28<option value="28" >Gold</option>sub1 29<option value="29" >Silver</option></optgroup><optgroup label='Pendants' >Select * from sub_categories 
                            where category_id='6'sub1 19<option value="19" >Diamond</option>sub1 20<option value="20" >Gold</option>sub1 21<option value="21" >Silver</option></optgroup><optgroup label='Brooches' >Select * from sub_categories 
                            where category_id='7'sub1 5<option value="5" >Silver</option>sub1 22<option value="22" >Gold</option>sub1 23<option value="23" >Diamond</option></optgroup><optgroup label='Charms' >Select * from sub_categories 
                            where category_id='8'sub1 24<option value="24" >Diamond</option>sub1 25<option value="25" >Gold</option>sub1 26<option value="26" >Silver</option></optgroup></br></div></div></br>

</select>
4

1 回答 1

1

您需要在之前添加一个空格selected

echo " selected";

没有它,“选定”将成为价值的一部分。您还应该引用您对 html 正确性的价值。

echo "<option value=\"$sub_category_id\"";

更新:

看起来你错过了一个查询?$sub_category_id2 被设置为一个未定义的变量:

    $select_query=  "select * from products LEFT JOIN product_description 
    ON products.product_id='".$id."' and 
    product_description.product_id='".$id."' 
    where products.product_id='".$id."'     ";

    // MISSING QUERY HERE - $fetch_select is not defined

    $sub_category_id2 = $fetch_select['sub_category_id'];
于 2013-09-09T05:30:48.777 回答