php - 当我更新 session 数组中的值时,它们会被清除

标签 php mysql e-commerce session-variables shopping-cart

我正在构建一个购物车,但遇到了一个我无法解决的问题。

当我将更新的数量保存到购物车中时,它会清除 $_SESSION['cart'] 中存储的值(我更新的值除外)。

<?php
session_start();

$new = $_GET['new']; //new product entry into cart

if($new){
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
        $_SESSION['items'] = 0;
        $_SESSION['total_price'] = '0.00';
    }
    if(isset($_SESSION['cart'][$new])){
        $_SESSION['cart'][$new]++;
    }else{
        $_SESSION['cart'][$new] = 1;
    }
    //$_SESSION['total_price'] = calculate_price($_SESSION['cart']);
    //$_SESSION['items'] = calculate_items($_SESSION['cart']);
};

if(isset($_POST['save'])){                                -----
    foreach($_SESSION['cart'] as $newId=>$qty){                |
        if($_POST[$newId] == '0'){                             | I think the
            unset($_SESSION['cart'][$newId]);                  | problem is
        } else {                                               | here?
            $_SESSION['cart'][$newId] = $_POST[$newId];        | 
        }                                                 -----
    }
    //run price and item functions
};

if(($_SESSION['cart']) && (array_count_values($_SESSION['cart']))){
    $cart = $_SESSION['cart'];
    foreach($cart as $newProd=>$qty) {
        require 'connect.inc.php';
        $query = "SELECT `product_id`, `new_id`, `name`, `price`, `image` FROM `products` WHERE `new_id` = '$newProd'";
        if ($query_run = mysql_query($query)) {
            while ($query_row = mysql_fetch_assoc($query_run)) {
                $product = $query_row['name'];
                $price = $query_row['price'];
                $image = $query_row['image'];
                $productId = $query_row['product_id'];
                $newId = $query_row['new_id'];
$tile = <<<TILE
    <article class="itemCart">
        <h1>$product</h1>
        <img src="$image">    
        <p>$price GBP</p>
        <form action="shopping-cart.php" method="post"><label for="$newId">Quantity </label>
        <input id="cartQty" type="number" value="$qty" max="30" min="0" name="$newId"></br>
        <input type="submit" name="save" value="Update"></form>
    </article>
TILE;
    echo $tile;
            }
        } else {
            echo mysql_error();
        }
    }
}else{
    echo '<h2>No items in your shopping cart</h2>';
};

最佳答案

我已经明白了!我在回应<form>为购物车中的每件商品添加标签。

$tile = <<<TILE
<article class="itemCart">
    <h1>$product</h1>
    <img src="$image">    
    <p>$price GBP</p>
    <form action="shopping-cart.php" method="post"><label for="$newId">Quantity </label>
    <input id="cartQty" type="number" value="$qty" max="30" min="0" name="$newId"></br>
    <input type="submit" name="save" value="Update"></form>
</article>
TILE;
echo $tile;

我已将整个脚本包含在 <form action="shopping-cart.php" method="post"> 中和</form> 。我将使用 ajax 改进此购物车,以便我可以更新购物车,而无需每次刷新页面...当我有时间学习它时。

希望这可以帮助其他犯过与我相同错误的人。

关于php - 当我更新 session 数组中的值时,它们会被清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34923804/

相关文章:

c# - 与 QuickBooks 成功连接的最佳 Dot.Net 电子商务开源

javascript - Codeigniter 更新表单未提交

php - 非常简单的ajax请求返回[object Object]

mysql - GROUP BY查询优化

mysql - 安排导出到文本文件

php - 如何使用 WorldPay 创建返回页面设置?

objective-c - 我有一个用 Objective-c 编写的 iOS 应用程序和一个使用 WooCommerce 的 Wordpress 网站。我怎样才能连接它们?

php - 如何在 Yii 中使用 POST 方法创建链接?

php - SSH 隧道 - MySQL 服务器已消失

mysql - mysql查询中的复杂搜索问题