简体   繁体   中英

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)

This is my code in 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

    <?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. 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. I just need to focus on mysql_function for now. Thanks for understanding.

You need to have executed 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.

Your form in purchase.php is posting to cart.php , so this code in purchase.php will not be run:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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