gpt4 book ai didi

javascript - 刷新页面后产品自动添加到购物车

转载 作者:行者123 更新时间:2023-11-28 04:10:08 24 4
gpt4 key购买 nike

我正在尝试编写购物车的代码,该代码正在运行。但是,当我刷新页面时,它会自动将产品添加到我的购物车。

例如,我的网站上有 3 个产品,分别是 Apple、Banana、Orange。我点击 Apple,它已添加到我的购物车,数量为 1,并且 URL 显示

`mydomain.com/my-cart?action=addcart&p_id=FylvGt6Yyb6n%2BzTXcJHwjBawOY%2Fw3QSZxF7rdUJLqhA%3D#`

现在,如果我刷新页面,就会将另一个 Apple 添加到我的购物车(数量 2)。然后,如果我再次刷新页面,它会添加另一个 Apple (数量 3),依此类推。我不知道为什么会这样。它正在添加到 SESSION。

你能帮我吗?

下面是我的购物车代码。

    $action = isset($_GET['action'])?$_GET['action']:"";
$p_id=$conn->real_escape_string($_GET['p_id']);
$decrypted_p_id = decryptIt($p_id);
//Add to cart
if($action=='addcart') {
//Finding the product by code
$query = "SELECT p_unique_id, p_images,p_name, p_currentprice FROM products WHERE p_id=?";
if ($stmt = $conn->prepare($query)) {
$stmt->bind_param("i", $decrypted_p_id);
$stmt->execute();
$stmt->bind_result($p_unique_id,$p_images, $p_name, $p_currentprice);
$stmt->fetch();

}
$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1; //Incrementing the product qty in cart
$_SESSION['products'][$decrypted_p_id] =array(
'qty'=>$currentQty,
'p_unique_id'=>$p_unique_id,
'p_images'=>$p_images,
'p_name'=>$p_name,
'p_currentprice'=>$p_currentprice
);
$product='';
// header("Location:cart.php");
}

显示产品

  <?php if(!empty($_SESSION['products'])):
foreach($_SESSION['products'] as $key=>$product):?>
/*some code here*/
endforeach;?>
<?php endif;?>

ADyson 建议的此处编辑代码

    if (isset($_POST['submit'])) {
$action=$conn->real_escape_string($_POST['action']);
$decrypted_p_id=$conn->real_escape_string($_POST['p_id']);
// whole code here

最佳答案

我找到了这段代码:

$currentQty = $_SESSION['products'][$decrypted_p_id]['qty']+1;

当您重新加载页面时,此代码始终运行,如果你想手动添加,请给出一些条件

关于javascript - 刷新页面后产品自动添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369021/

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