我创建了一个'Insert_Product'页面,其中有一些产品相关字段和一个下拉框,显示特定产品所涉及的'Category'和'Sub_category'。
当我更新任何特定产品时,它会打开产品页面并从表单字段中获取数据库中的数据,但在下拉列表中显示第一个给定名称。我希望它选择产品所属的选定类别。
现在它显示数据库中的第一项。
$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>
答案 0 :(得分: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'];