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

时间:2015-03-11 16:27:32

标签: php mysql session shopping-cart

我正在创建一个有两个网页的购物车; purchase.php(您可以在其中查看所有可用产品列表并将其添加到购物车中)和cart.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>

这个是我的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>

当我点击“添加到购物车”按钮时,我被重定向到我的cart.php,但项目没有显示。我担心我的购物车会话代码不正确,但我不知道如何解决这个问题

PS让我们暂时忘记SQL注入以及PDO和Mysqli_function。我现在只需要关注mysql_function。谢谢你的理解。

1 个答案:

答案 0 :(得分:0)

您需要在使用该会话的所有文件中执行session_start();。将其添加到purchase.php的顶部,或将其放入另一个文件中并在两个文件中都需要它。

purchase.php中的表单已发布到cart.php,因此purchase.php中的此代码将无法运行:

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