页面刷新后PHP会话数据丢失

时间:2016-08-28 14:18:08

标签: php html session refresh cart

我正在使用此产品页面。基本上它按ID显示产品,并为您提供将其添加到购物车的选项。我的问题是,每次刷新页面时,存储在$ _SESSION中的数据都会丢失。

<?php 
    include 'scripts\init.php'; // contains session_start(); and the functions
    if(!IsProductIdSafeAndExisting()) 
    {
        session_write_close();
        header("Location: shop.php");
        die();
    }
    if(isset($_POST['quantity'])) // adds current item to cart but gets lost after refresh
        AddItemToCart($_GET["id"],$_POST['quantity']);

    $id             = $_GET["id"];
    $name           = GetProductField($id,"name");
    $image          = GetProductField($id,"image");
    $price          = GetProductField($id,"price");
    $stock          = GetProductField($id,"stock");
    $details        = GetProductField($id,"details");
    $total_products = GetTotalProducts();
    $total_price    = GetTotalProductsPrice();

    LoadHeaderByTitle($name." | Magazin Vinuri");
 ?>
<body>
    <div id="page">
        <?php LoadNavigationBy(""); ?>
        <div id="body">
            <div class="header">
                <div>
                    <h1><?php echo $name; ?></h1>
                </div>
            </div>
            <div class="singlepost">
                <div class="featured">
                    <img src="images/<?php echo $image; ?>" alt="">
                    <h1><?php echo $name.' - '.$price.' lei'; ?></h1>
                    <span>Mai sunt <?php echo '<strong>'.$stock.'</strong>'; ?> bucati ramase.</span>
                    <p><?php echo $details; ?></p>
                    <div class="additem">
                    <center>
                        <form method="POST" action="product.php?id=<?php echo $id; ?>">
                            <input type="text" name="quantity" value="Cantitate" onblur="this.value=!this.value?'Cantitate':this.value;" onfocus="this.select()" onclick="this.value='';">
                            <input type="submit" value="Adauga" id="submit">
                        </form>
                        </center>
                    </div>
                </div>

                <div class="sidebar">
                    <h1>Cosul tau</h1>
                    <img src="images/cart.png" alt="">
                    <h2><?php echo $total_price; ?> lei</h2><br>
                    <p>Momentan aveti <strong><?php echo $total_products; ?></strong> produse in cos. Pentru a edita lista de produse dati click pe butonul de mai jos.</p>
                    <a href="cart.php" class="more">vezi cumparaturi</a>
                </div>
            </div>
        </div>
<?php include 'scripts\overall\foot.php' ?>

的init.php

<?php
    session_start();
    require 'database\connect.php';
    require 'functions\general.php';

    require 'functions\engine.php';
?>

1 个答案:

答案 0 :(得分:0)

问题是我存储了这样的产品ID $ _SESSION [10] =某事;这是错误的,因为它和$ _SESSION一样[&#39; 10&#39;] =某事;所以我把它改成$ _SESSION [&#39; id_10&#39;] =某事;现在它可以工作