php - 将购物车项目保存到数据库中
问题描述
我制作了一个基本的购物网站,客户可以在其中将商品添加到他们的购物车然后查看他们的购物车,他们必须能够结帐并且购物车中的商品必须插入数据库。
这是我用来将我的项目添加到我的代码,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
}
?>
解决方案
我已经解决了我的问题,这是一个非常愚蠢的问题,我的结帐按钮上从来没有<form method="post"></form>
,现在所有内容都正确插入到数据库中。感谢所有帮助过我的人,这将真正帮助我改进我的代码。
推荐阅读
- javascript - 为什么在分叉的Node进程完成执行脚本后节点保持进程处于活动状态
- apache-flink - 读取 Flink 中追加的文件
- android - 更新具有相同签名的 apk 后再次出现被 Play Protect 阻止
- css - CSS 卡片文本在动画上更改/移动
- c++ - C++中迭代器值的垃圾值
- rest - 为什么以及何时应该使用身份提供程序,例如 Auth0 或 Azure AD B2C,而不是仅将用户凭据存储在我的数据库中?
- flutter - flutter_local_notifications 有 onShown 监听器吗?
- sockets - UDP 与 TCP 套接字要求
- spring-mvc - java.lang.ClassCastException:[Ljava.lang.Object;不能施放
- node.js - 一个把手#each 不适用于 Heroku