dongyong6428 2013-05-21 10:45
浏览 24

PHP下拉选中

i've got 3 drop down boxes which populate each drop down from the one above but on the 3rd drop down i want it to bring back a value that i selected. The code is below

<tr>
<td width="119">Category</td>

<td width="371">
   <select name="cat_id" id="cat_id" onChange="showCompany(this.value);">
   <option value="">--Select--</option>
   <?php
    $sql="select * from category";
   $sql_row=mysql_query($sql);
   while($sql_res=mysql_fetch_assoc($sql_row))
   {
   ?>
   <option value="<?php echo $sql_res["id"]; ?>" <?php if($sql_res["id"]   ==$_REQUEST     ["cat_id"]) { echo "Selected"; } ?>><?php echo $sql_res["category_name"]; ?  ></option>
    <?php
    }
   ?>
   </select>
   </td>
 </tr>

 <tr>
<td>Company</td>
<td id="td_company">
   <select name="company_id" id="company_id">;
   <option value="">--Select--</option>
   <?php
   $sql="select * from company where cat_id='$_REQUEST[cat_id]'";
   $sql_row=mysql_query($sql);
   while($sql_res=mysql_fetch_assoc($sql_row))
   {
   ?>
   <option value="<?php echo $sql_res["id"]; ?>"><?php echo $sql_res["company_name"]; ?></option>
   <?php
   }
   ?>
 </select>
   </td>
 </tr>
 <tr>

<td>Item</td>
<td id="td_item">
   <select name="item_id" id="item_id">
   <option value="">--Select--</option>
   <?php
   $sql="select * from item where comp_id='$_REQUEST[cat_id]'";
   $sql_row=mysql_query($sql);
   while($sql_res=mysql_fetch_assoc($sql_row))
   {
   ?>
   <option value="<?php echo $sql_res["id"]; ?>"><?php echo $sql_res["item_name"]; ?></option>
   <?php
   }
   ?>
</select>
   </td>
 </tr>
 <tr>
<td>Price</td>
<td id="td_item">
   <?php
   $sql="select unit_cost from item where comp_id='$_REQUEST[cat_id]'";
   $sql_row=mysql_query($sql);
   while($sql_res=mysql_fetch_assoc($sql_row))
   {
   echo "<td>" . "&pound" . $sql_res['unit_cost'] . "</td>";
   }
   ?>

   </td>
 </tr>

 <tr>
 </tr>
</table>
</form>

so basically i want it to bring back the price from the option i selected, at the moment its bringing back all prices.

e.g. first drop down i select cellings then the company drop down i select Access Panels then 3rd drop down they is 3 items (Arm Strong, Track and Screw) and it brings back all 3 prices where i want to be able to select one and only bring back that price. any ideas?

  • 写回答

1条回答 默认 最新

  • doujiu8145 2013-05-29 21:18
    关注

    First off you shouldn't be using mysql_* functions, they're deprecated. Look into either mysqli or PDO. Second, you have a vulnerability in your code where you are inserting $_REQUEST straight into your SQL. That's called SQL Injection. You should probably do some reading on it.

    With all that in mind the problem is your query is trying to select all of the items where company id = category id. This is your query:

    $sql="select * from item where comp_id='$_REQUEST[cat_id]'";
    

    Notice the comp_id='$_REQUEST[cat_id]' should it instead be something like comp_id = $_REQUEST[comp_id]'?

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了