无法从mysql查询中检索多行

时间:2016-07-02 20:21:59

标签: php mysql

我正在用PHP制作酒店在线订购系统。我有问题,当我在购物车中添加一个项目时,原价是2美元但是还有2个选项(1)添加奶酪(2)添加更多鸡肉。

显示主要项目和价格水。但它只显示了一个额外的选项奶酪,我已经选择了两者。

修复了它 - 我的更新代码如下:

<?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 个答案:

答案 0 :(得分:1)

顺便说一下,并不是说它有多大帮助,但所有查询都可以写成如下,我认为这更容易阅读......

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

实际上(并忽略了注入/弃用的API等),此代码中的所有SQL都可以重写如下...

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')

但实际上,这个查询远离你需要的地方。所以看看一些在线教程,或者一本关于PHP和MySQL的好书 - 虽然我有一种令人沮丧的感觉,我的评论会被置若​​罔闻。