gpt4 book ai didi

php - 如何通过点击结帐按钮来更新产品数量的值?

转载 作者:行者123 更新时间:2023-11-29 22:14:16 26 4
gpt4 key购买 nike

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

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

}

因此,当我单击结帐按钮时,在 checkout.php 上应该有每种产品的正确数量。

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

最佳答案

如果我理解正确的话,你一定会做类似的事情。

购物车.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>";

}

结帐.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;
}

关于php - 如何通过点击结帐按钮来更新产品数量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com