简体   繁体   English

购物车中无法使用$ _SESSION添加数据或未显示

[英]Unable to add data or is not showing using $_SESSION in shopping cart

I'm creating a shopping cart where there are two web pages; 我正在创建一个有两个网页的购物车; the purchase.php (where you can see all the list of available products and add them to cart) and the cart.php (where you will be able to view all the items you've added and remove an item or clear all the cart, and the total of an item) Purchase.php(您可以在其中查看所有可用产品的所有列表并将其添加到购物车)和cart.php(您可以在其中查看已添加的所有项目并删除一个项目或清除所有购物车) ,以及一项的总和)

This is my code in purchase.php: 这是我在purchase.php中的代码:

<?php session_start();
?>
<!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Purchase</title>

    <link rel = "stylesheet" href="bootstrap/css/bootstrap.css">
        <!-- Styles -->
        <link href="css/bootstrap-combined.min.css" rel="stylesheet">
        <link href="datatable-bootstrap.css" rel="stylesheet">
        <!-- JS -->
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/jquery.dataTables.min.js"></script>
        <script src="datatable-bootstrap.js"></script>

    <?php
    require('config/db_conn.php');


    $fetch = mysql_query("SELECT * FROM product") or die(mysql_error());





    if(!empty($_POST["add"])) {
                $productByCode = mysql_query("SELECT * FROM product WHERE p_code='" . $_POST["p_code"] . "'");
                $itemArray = array($productByCode["p_code"]=>array('name'=>$productByCode["p_name"], 'code'=>$productByCode["p_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["p_price"]));

                if(!empty($_SESSION["cart_item"])) {
                    if(in_array($productByCode["p_code"],$_SESSION["cart_item"])) {
                        foreach($_SESSION["cart_item"] as $k => $v) {
                                if($productByCode["p_code"] == $k)
                                    $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                        }
                    } else {
                        $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
                    }
                } else {
                    $_SESSION["cart_item"] = $itemArray;
                }
            } 


    ?>
    </head>
    <style type="text/css">
    body
    {
    background-color: #6BBEE4;
    }

    th
    (
    text-align:center;
    )

    td
    (
    align:center;
    )

    .text-center
    {
        text-align: center !important;
    }

    img
    {
        height: 200px;
        width: 200px;
    }

    .hide
    {
        visibility: none;
    }

    .quantitySize
    {
        width: 90px;
    }

    .pull
    {
        margin-left: 220px;
        margin-top: -360px;
        position: absolute;
    }

    </style>
    <body>



    <nav class="navbar navbar-inverse ">
      <div class="container-fluid ">
        <div class="navbar-header">
          <a class="navbar-brand" href="home.php" >MyComputer</a>
        </div>
        <div>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="home.php">Home</a></li>
            <li class="active"><a href="purchase.php">Purchase</a></li>     
            <li><a href="cart.php"><span class="glyphicon glyphicon-shopping-cart"></span> Cart</a></li>
            <li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>

        </div>
      </div>
    </nav>





            <?php while($result=mysql_fetch_array($fetch))  { ?>
            <div class="well form-container-">
                <form method = "post" action="cart.php?action=add&code=<?php echo $result['p_code']?>">
                    <div class="form-group"><img src="<?php echo $result['p_image']?>"></div>
                    <div class="form-group"><?php echo $result['p_name']?></div>
                    <div class="form-group"><?php echo $result['p_code']?></div>
                    <div class="form-group"><?php echo $result['p_price']?></div>
                    <div class="form-group"><input class="quantitySize" type="text" name="quantity" value="1"/>&nbsp;&nbsp;&nbsp;&nbsp;<input type = "submit" class="btn btn-primary" name="add" value="Add to Cart"></div>
                </form>
                <form class="pull">
                <div class="form-group"><p><?php echo $result['p_desc']?></p></div>
                </form>
            </div>
            <?php }?>
        </div>

    </body>
    </html>

This one is for my cart.php 这是给我的cart.php

    <?php
    session_start();

    if($_SESSION['username']=="")
    {
    header("location: login.php");
    }

    if(!empty($_GET["action"])) {
    switch($_GET["action"]) {
        case "add";
    if(!empty($_POST["add"])) {
        $productByCode = mysql_query("SELECT * FROM product WHERE p_code='" . $_POST["p_code"] . "'");
        $itemArray = array($productByCode["p_code"]=>array('name'=>$productByCode["p_name"], 'code'=>$productByCode["p_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode["p_price"]));

        if(!empty($_SESSION["cart_item"])) {
            if(in_array($productByCode["p_code"],$_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($productByCode["p_code"] == $k)
                            $_SESSION["cart_item"][$k]["quantity"] = $_POST["quantity"];
                }
            } else {
                $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray);
            }
        } else {
            $_SESSION["cart_item"] = $itemArray;
        }
    } 
break;
        case "remove":
            if(!empty($_SESSION["cart_item"])) {
                foreach($_SESSION["cart_item"] as $k => $v) {
                        if($_GET["code"] == $k)
                            unset($_SESSION["cart_item"][$k]);              
                        if(empty($_SESSION["cart_item"]))
                            unset($_SESSION["cart_item"]);
                }
            }
        break;
        case "empty":
            unset($_SESSION["cart_item"]);
        break;  
    }
    }
    ?>


    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>MyCart</title>

    <link rel = "stylesheet" href="bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>

    </head>
    <style type="text/css">

    body
    {
    background-color: #6BBEE4;
    }

    </style>
    <body>




    <nav class="navbar navbar-inverse ">
      <div class="container-fluid ">
        <div class="navbar-header">
          <a class="navbar-brand" href="home.php" >MyComputer</a>
        </div>
        <div>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="home.php">Home</a></li>
            <li><a href="purchase.php">Purchase</a></li>     
            <li class="active"><a href="cart.php"><span class="glyphicon glyphicon-shopping-cart"></span> Cart</a></li>
            <li><a href="logout.php"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>

        </div>
      </div>
    </nav>
    <div id="shopping-cart">
    <div class="txt-heading">Shopping Cart <input class="pull-right" type="button" name="empty" value="Empty Cart"></div>
    <?php
    if(isset($_SESSION["cart_item"])){
        $item_total = 0;
    ?>  
    <table class="table table-striped datatable " id="example" border="1" >
                <thead align="center">
                    <tr>
                        <th class="text-center">Product Name</th>
                        <th class="text-center">Image</th>
                        <th class="text-center">Code</th>
                        <th class="text-center">Description</th>
                        <th class="text-center">Price</th>
                        <th class="text-center">Action</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($_SESSION["cart_item"] as $item){ ?>
                    <tr>
                        <td align="center"><?php echo $item['p_name']?></td>
                        <td align="center"><img src="<?php echo $item['p_image']?>"></td>
                        <td align="center"><?php echo $item['p_code']?></td>
                        <td align="center"><?php echo $item['p_desc']?></td>
                        <td align="center"><?php echo "$" .$item['p_price']?></td>
                        <td align="center"><input type = "button" class="btn btn-primary" name="remove" value="Remove"></td>
                    </tr>
                    <?php
                        $item_total += ($item['p_price']*$item["quantity"]);
                    } ?>
                <tr>
                        <td colspan="5" align=right><strong>Total:</strong> <?php echo "$".$item_total; ?></td>
                </tr>
                </tbody>
    </table>        
                <?php
                    }
                ?>
    </div>
    </head>
    </body>
    </html>

When I click the "Add to Cart" button, I got redirected to my cart.php but the items are not showing. 当我单击“添加到购物车”按钮时,我被重定向到我的cart.php,但未显示项目。 I'm afraid my codes for the session for my cart are incorrect but I don't know how to fix this 恐怕我的购物车交易代码不正确,但我不知道如何解决

PS Let's just forget about the SQL injection for the time being as well as the PDO and Mysqli_function. PS让我们暂时忘记SQL注入以及PDO和Mysqli_function。 I just need to focus on mysql_function for now. 我现在只需要关注mysql_function。 Thanks for understanding. 感谢您的理解。

You need to have executed session_start(); 您需要执行session_start(); in all files that use the session. 在使用该会话的所有文件中。 Add it to the top of your purchase.php , or put it in another file and require it in both files. 它添加到您的顶部purchase.php ,或把它放在另一个文件,它需要在这两个文件。

Your form in purchase.php is posting to cart.php , so this code in purchase.php will not be run: 您在形式上purchase.php被张贴到cart.php ,所以这段代码在purchase.php将不会运行:

if(!empty($_POST["add"])) {
//code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM