doulong2782 2016-07-02 20:21
浏览 44

无法从mysql查询中检索多行

I am making hotel online order system in php. I am having issue when i have added one item in the cart the original price is 2$ but with that there were 2 more options (1) add cheese (2) add more chicken.

it is showing the main item and price watever it is. But it is showing only one extra option cheese and i have selected both.

fixed it - my updated code is below:

<?php
$rest_menuC = mysql_query("select * from menu_items where status = 1 order by id");
while ($mitemC  = mysql_fetch_array($rest_menuC)){
    $mitem_idC  = $mitemC['id'];
    $mitem_enC  = $mitemC['ename'];
    $mitem_anC  = $mitemC['aname'];
    $mitem_fnC  = $mitemC['filename'];
    $mitem_ssC  = $mitemC['status'];
$order_temp =   mysql_query("select * from temp_cart where item_id = '".$mitem_idC."'  order by id");
while ($torder = mysql_fetch_array($order_temp)) {
    $prITTD     =   $torder['id'];
    $prITTC     =   $torder['item_id'];
    $prIDTC     =   $torder['price_id'];
    $qtyT       =   $torder['qty'];     
    $pc1        =   $torder['pc1'];
    $pc2        =   $torder['pc2'];

$order_tempCHP      =   mysql_query("select * from choice_price
WHERE
id='".$pc1."' OR id='".$pc2."' OR id='".$pc3."' OR id='".$pc4."' OR id='".$pc5."' OR id='".$pc6."' OR 
id='".$pc7."' OR id='".$pc8."' OR id='".$pc9."' OR id='".$pc10."' OR id='".$pc11."' OR id='".$pc12."'
AND item_id = '".$prITTC."'");
$toarder = mysql_num_rows($order_tempCHP);
while ($torderCP    = mysql_fetch_assoc($order_tempCHP)) {
    $CPid   =   $torderCP['id'];
    $CPenm  =   $torderCP['ename'];
    $CPicd  =   $torderCP['choice_id'];
    $CPitd  =   $torderCP['item_id'];
    $CPlpr  =   $torderCP['price'];

if ($toarder == 1){
?>
<span class="quantity"><font color="red" style="font-weight:bold;">Choices # <?php echo $CPicd; ?></font></span><br>
<span class="quantity"> <?php echo $CPenm; ?> # <?php echo $CPlpr; ?></span>

<?php }else{ ?>

<span class="quantity"><font color="red" style="font-weight:bold;">Choices # <?php echo $CPicd; ?></font></span><br>
<span class="quantity"> <?php echo $CPenm; ?> # <?php echo $CPlpr; ?></span><br>
<?php } } ?>
  • 写回答

1条回答 默认 最新

  • duancan1950 2016-07-02 22:55
    关注

    Incidentally, and not that it helps much, but all of that query could be written as follows, which I think is easier to read...

    SELECT * 
      FROM choice_price
     WHERE id IN('$pc1','$pc2','$pc3','$pc4','$pc5','$pc6','$pc7','$pc8','$pc9','$pc10','$pc11','$pc12')
       AND item_id = $prITTC;
    

    In fact (and ignoring injection/deprecated APIs, etc), all of the SQL in this code could be re-written as follows...

    SELECT p.id
         , p.ename
         , p.choice_id
         , p.item_id
         , p.price
      FROM menu_items i
      JOIN temp_cart c 
        ON c.item_id = i.id
      JOIN choice_price p
        ON p.item_id = c.item_id
     WHERE i.status = 1 
       AND p.id IN('$pc1','$pc2','$pc3','$pc4','$pc5','$pc6'
                  ,'$pc7','$pc8','$pc9','$pc10','$pc11','$pc12')
    

    But really, this query is far, far away from where you need to be. So have a look at some online tutorials, or a decent book on PHP and MySQL - although I have a depressing feeling that my comments will fall on deaf ears.

    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?