douduonang3169 2017-12-05 05:07
浏览 173
已采纳

如何正确循环和更新数据库中的数量。

Hello I am currently having problems updating my database quantities with a for loop. Right now i created a shopping cart and when the user purchases a item i want update the quantities in my database. The problem is i can only insert the last product id in the shopping so only 1 of the items update even though i have it in a loop. The code is long so im only going to show the part that i am having trouble with.

  for($i=0; $i<count($cart); $i++){
   $s += $cart[$i]->price * $cart[$i]->quantity;
   $tax= $s*.05;
   $shipping=$s* .02;
  $total=$s+$tax+$shipping;
   ?>
 <tr>
    <td><?php echo $cart[$i]->id; ?></td>
    <td><?php echo $cart[$i]->name; ?></td>
    <td><?php echo $cart[$i]->price; ?></td>
    <td><?php echo $cart[$i]->quantity; ?></td>
    <td><?php echo $cart[$i]->price * $cart[$i]->quantity; ?></td>
  </tr>

  <?php 
 $index++;
 $quantitycart=$cart[$i]->quantity;

   }
      if(array_key_exists('submit', $_POST))
      {  $results = mysqli_query($con, "select * from products"); 

     while($products= mysqli_fetch_object($results)){
     for($i=0; $i<count($cart); $i++){    
     $quantity= $products->quantity;
     $idcart=$cart[$i]->id;


     $sql = "UPDATE products SET quantity= quantity -'$quantitycart' WHERE 
 id='$idcart'";
    }}
 if ($con->query($sql) === TRUE) {
    echo "Record updated successfully";
 } else {
    echo "Error updating record: " . $con->error;
}

The problem is with the $idcart. For some reason it isn't properly looping and giving the right id's only the last one in the cart too. Any help would be appreciated.

Thank you.

  • 写回答

1条回答 默认 最新

  • douhui8454 2017-12-05 05:26
    关注

    The problem lies in the loop, where your sql query will be got over written in each loop so the last sql query will be executed. So you can append the sql query each time and execute it using multi_query method

        if(array_key_exists('submit', $_POST))
          {  $results = mysqli_query($con, "select * from products"); 
         $sql="";//init
         while($products= mysqli_fetch_object($results)){
         for($i=0; $i<count($cart); $i++){    
         $quantity= $products->quantity;
         $idcart=$cart[$i]->id;
         $sql .= "UPDATE products SET quantity= quantity -'$quantitycart' WHERE 
         id='$idcart';";//here u need to append the query
        }//end for
        }//end while
        if ($con->multi_query($sql) === TRUE) {//use multi_query
        echo "Record updated successfully";
        } else {
        echo "Error updating record: " . $con->error;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?