首页 > 解决方案 > 将购物车项目保存到数据库中

问题描述

我制作了一个基本的购物网站,客户可以在其中将商品添加到他们的购物车然后查看他们的购物车,他们必须能够结帐并且购物车中的商品必须插入数据库。

这是我用来将我的项目添加到我的代码,cart.php使用会话数组作为可以添加到购物车的多个项目。

<?php 
    session_start();
    include("DBConn.php");
    $msg = "";

    if(isset($_POST["add_to_cart"]))
    {
        if(isset($_SESSION["shopping_cart"]))
        {
            $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
            if(!in_array($_GET["id"], $item_array_id))
            {
                $count = count($_SESSION["shopping_cart"]);
                $item_array = array(
                    'item_id'           =>  $_GET["id"],
                    'item_name'         =>  $_POST["hidden_item"],
                    'item_price'        =>  $_POST["hidden_price"],
                    'item_quantity'     =>  $_POST["quantity"]
                );
                $_SESSION["shopping_cart"][$count] = $item_array;
            }
            else
            {
                echo '<script>alert("Item Already Added")</script>';
                echo '<script>window.location="book_a_venue.php"</script>';
            }
        }
        else
        {
            $item_array = array(
                    'item_id'           =>  $_GET["id"],
                    'item_name'         =>  $_POST["hidden_item"],
                    'item_price'        =>  $_POST["hidden_price"],
                    'item_quantity'     =>  $_POST["quantity"]
            );
            $_SESSION["shopping_cart"][0] = $item_array;
        }
    }
?>

这是我用来从会话中检索项目并显示它的代码,每个项目都以动态创建的形式显示。这是我想插入到我的数据库中以保存订单的项目。

<?php
    if(!empty($_SESSION["shopping_cart"])){
        $total = 0;
        foreach($_SESSION["shopping_cart"] as $keys => $values)
        {
?>
                                    
<form method="post" action="cart.php?action=remove&id="id" class="cart-items">
    <div class="border rounded">
        <div class="row bg-white">
           <div class="col-md-6">
              <h4 class="text-info"><?php echo $values["item_name"]; ?></h4>
              <h4 class="text-danger">Price: R <?php echo $values["item_price"]; ?></h4>
              </br>
              <a href="cart.php?action=delete&id=<?php echo $values["item_id"]; ?>"><span class="btn btn-danger mx2">Remove</span></a>
            </div>
            <div class="col-md-3 py-5">
               <div>
               <h5>Quantity</h5>
               </br>
               <h4 name="quantity" class="text-info"><?php echo $values["item_quantity"]; ?></h4>
               <h3>R <?php echo number_format($values["item_quantity"] * $values["item_price"], 2);?></h3>
            </div>
          </div>
       </div>
    </div>
</form>

<?php
    $total = $total + ($values["item_quantity"] * $values["item_price"]);
    }
}
?>

这是我为了将购物车中的物品插入数据库而尝试做的。这不起作用,可能完全错误。

<?php
    if (isset($_POST['submit'])){
        
        if(!empty($_SESSION["shopping_cart"])){
            $total = 0;
            
            
            
            foreach($_SESSION["shopping_cart"] as $keys => $values)
            {
            $stmt = $con->prepare("INSERT INTO tbl_order (`item`, `price`, `quantity`, `totalprice`, `total`) VALUES (?, ?, ?, ?, ?)");
            $stmt->bind_param("sss", $item, $price, $quantity, $totalprice, $total);
            
            $item = $values["item_name"];
            $price = $values["item_price"];
            $quantity = $values["item_quantity"];
            $totalprice = $values["item_quantity"] * $values["item_price"];
            $total = $total + ($values["item_quantity"] * $values["item_price"]);
            $stmt->execute();
            }           
            $stmt->close();
            $con->close();          
        }   
?>

<div class="col-md-4 offset-md-1 border rounded mt-5 bg-white h-25">
  <div class="pt-4">
    <h6>
        PRICE DETAILS
    </h6>
    <hr>
        <div class="row price-details">
            <div class="col-md-6">
            <h6>
                Delivery Charges
            </h6>
            <hr>
            <h6>
            Amount Payable
            </h6>
            <hr>
            <h6>
            Grand Total
            </h6>
            <hr>
            <input type="button" value="Continue Shopping" class="btn btn-warning" id="btnHome" onClick="document.location.href='book_a_venue.php'"/>   
        </div>
                
        <div class="col-md-6">
            <h6 class="text-success">
                FREE
            </h6>
            <hr>
            <h6>
            R <?php echo number_format($total, 2);?>
            </h6>
            <hr>
            <h6>
            R <?php echo number_format($total, 2);?>
            </h6>
            <hr>
            <button type="submit" name="submit" class="btn btn-danger mx-2">Check Out</button>
        </div>
    </div>
</div>
</div>
<?php
    }
?>

标签: phphtml

解决方案


我已经解决了我的问题,这是一个非常愚蠢的问题,我的结帐按钮上从来没有<form method="post"></form> ,现在所有内容都正确插入到数据库中。感谢所有帮助过我的人,这将真正帮助我改进我的代码。


推荐阅读