dongpin1059 2015-07-10 15:42
浏览 20

如何通过单击结帐按钮更新产品数量的值?

I have a little shop and what I want to do is update the quantity of my session after I changed the value in the input field.

cart.php:

if(count($_SESSION['cart_items'])>0){

        $product_ids = "";
        foreach($_SESSION['cart_items'] as $product_id=>$value){
            $product_ids = $product_ids . $product_id . ",";
        }

        $product_ids = rtrim($product_ids, ',');

        $query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";

        $stmt = $con->prepare( $query );
        $stmt->execute();


        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $quan = $_SESSION['cart_items'][$product_id];

            echo "{$name}";
            echo "<input type='number' value='{$quan}'>";   
        }

        echo "<form action='checkout.php' method='get'>
            <button> Checkout</button>
            </form>"; 

    }

So when I click on the checkout button, on checkout.php there should be the right quantity for each product.

checkout.php:

$query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";

    $stmt = $con->prepare( $query );
    $stmt->execute();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);

        $quan = $_SESSION['cart_items'][$product_id];

        echo "{$name}";
        echo "{$quan}";
    }
  • 写回答

1条回答 默认 最新

  • 普通网友 2015-07-10 18:58
    关注

    If I understood it correctly you must do something like that.

    cart.php

    if(count($_SESSION['cart_items'])>0){
    
        $product_ids = "";
        foreach($_SESSION['cart_items'] as $product_id=>$value){
            $product_ids = $product_ids . $product_id . ",";
        }
    
        $product_ids = rtrim($product_ids, ',');
    
        $query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";
    
        $stmt = $con->prepare( $query );
        $stmt->execute();
    
        echo "<form action='checkout.php' method='post'>";
    
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);
            $quan = $_SESSION['cart_items'][$product_id];
    
            echo "{$name}";
            echo "<input type='number' name='quantity_{$product_id}' value='{$quan}'>";   
        }
    
    
            echo "<input type='submit' value='Chekout'> Checkout</input></form>"; 
    
    }
    

    checkout.php:

    $query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";
    
    $stmt = $con->prepare( $query );
    $stmt->execute();
    
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
    
        $quan = $_SESSION['cart_items'][$product_id];
    
    $productStr = "quantity_" . {$product_id};
    $newQuant = $_POST[$productStr];
    
    echo "{$name}";
        echo "{$quan}";
        echo $newQuant;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度