Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何通过单击“签出”按钮更新产品数量的值?_Php_Mysql - Fatal编程技术网

Php 如何通过单击“签出”按钮更新产品数量的值?

Php 如何通过单击“签出”按钮更新产品数量的值?,php,mysql,Php,Mysql,我有一个小商店,我想做的是在更改输入字段中的值后更新会话的数量 cart.php: if(count($_SESSION['cart_items'])>0){ $product_ids = ""; foreach($_SESSION['cart_items'] as $product_id=>$value){ $product_ids = $product_ids . $product_id . ","; }

我有一个小商店,我想做的是在更改输入字段中的值后更新会话的数量

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>"; 

    }
$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}";
    }

如果我理解正确的话,你必须做那样的事

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>"; 

}
$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;
}