我创建了一个“插入产品”页面,其中有一些与产品相关的字段和一个下拉框,其中显示了与特定产品相关的“类别”和“子类别”。
当我更新任何特定产品时,它会打开产品页面并在表单字段中从数据库中获取数据,但在下拉列表中它会显示第一个给定名称。我希望它选择产品所属的选定类别。
现在它显示数据库中的第一个项目。
$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>