gpt4 book ai didi

php - 减少 MySQL 购物车中的商品数量

转载 作者:搜寻专家 更新时间:2023-10-30 22:30:50 28 4
gpt4 key购买 nike

我正忙于创建一个购物车 php 项目,我已经成功地完成了向购物车添加、删除和更新项目,但我遇到的问题是,如果用户单击“继续结帐”按钮,我想要的是在购物车中在数据库中相应地减少。例如,如果一个人在他的购物车中有:2 x 产品一3 x 产品二

如果销售完成,我希望减少我手头的数量。有人可以帮忙吗下面是我的购物车代码:

<?php
session_start();
@mysql_connect("localhost","root","") or die("Could not connect to database");
@mysql_select_db("bookstore") or die("Could not select database");
include("admin/php/myFunctions.php");

if(!empty($_GET['prodid'])){
$pid = $_GET['prodid'];
$wasFound = false;
$i = 0;
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$_SESSION["cart_array"]=array(0=>array("productID"=>$pid,"quantity"=>1));
}else{
foreach($_SESSION["cart_array"] as $each_product){
$i++;
while(list($key,$value)=each($each_product)){
if($key=="productID" && $value==$pid){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$pid,"quantity"=>$each_product ['quantity']+1)));
$wasFound=true;
}
}
}
if($wasFound==false){
array_push($_SESSION["cart_array"],array("productID"=>$pid,"quantity"=>1));
}
}
header("location:shoppingcart.php");
exit();
}
//-------------------------------------------------------------------------------------------------
$submit = $_POST['btnUpdate'];
if($submit == "Update"){
$x = 0;

foreach($_SESSION["cart_array"] as $each_product){
@$i++;
$quantity = $_POST['txtQuan'.$x];
$prodStock = $_POST['txtHoldQuan'.$x];
$prodAdjustId = $_POST['txtHoldProdId'.$x++];
if($quantity<1){ $quantity = 1; }
if($quantity>$prodStock){ $quantity = $prodStock; }
while(list($key,$value)=each($each_product)){
array_splice($_SESSION["cart_array"],$i-1,1,array(array("productID"=>$prodAdjustId,"quantity"=>$quantity)));
}
}


}
//-------------------------------------------------------------------------------------------------
if(!empty($_GET['cid']) || isset($_GET['cid'])){
$removeKey = $_GET['cid'];
if(count($_SESSION["cart_array"])<=1){
unset($_SESSION["cart_array"]);
}else{
unset($_SESSION["cart_array"]["$removeKey"]);
sort($_SESSION["cart_array"]);
}
}
//-------------------------------------------------------------------------------------------------
$cartTitle = "";
$cartOutput = "";
$cartTotal = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1){
$cartOutput="<h2 align='center'> Your shopping cart is empty </h2>";
}else{
$x = 0;
$cartTitle .= '<form name="shoppingcart_form" action="shoppingcart.php" method="post" /><table width="700px" cellspacing="0" cellpadding="5">
<tr bgcolor="#CCCCCC">
<th width="220" align="left">Image </th>
<th width="140" align="left">Name </th>
<th width="100" align="center">Quantity </th>
<th width="60" align="center">Stock </th>
<th width="60" align="right">Price </th>
<th width="60" align="right">Total </th>
<th width="90"> </th></tr>';
$i = 0;
foreach($_SESSION["cart_array"] as $each_product){
$product_id = $each_product['productID'];
$sql=mysql_query("select * from tblproduct where prod_id='$product_id' limit 1");
while($row=mysql_fetch_array($sql)){
$prodNo = $row["prod_no"];
$prodID = $row["prod_id"];
$prodName = $row["prod_name"];
$prodPrice = $row["prod_price"];
$prodQuan = $row["prod_quan"];
}
$pricetotal=$prodPrice*$each_product['quantity'];
$cartTotal= number_format($pricetotal+$cartTotal,2);
$cartOutput .= '<tr><td><img style="border: 2px solid;" src="images/product/'.$prodNo.'.jpg" width="150" height="120" /></td>
<td>'.$prodName.'</td>
<td align="center"><input type="hidden" name="txtHoldProdId'.$i.'" value="'.$prodID.'" /><input name="txtQuan'.$i.'" type="text" value="'.$each_product['quantity'].'" style="width: 40px; text-align: center" /> </td>
<td align="center"><input type="hidden" name="txtHoldQuan'.$i.'" value="'.$prodQuan.'" /> '.$prodQuan .' pcs</td>
<td align="right">R '.$prodPrice.'</td>
<td align="right">R '.$pricetotal.'</td>
<td align="center"> <a href="shoppingcart.php?cid='.$i++.'"><img src="images/remove_x.gif" alt="remove" /><br />Remove</a> </td></tr>';
}
$_SESSION['checkoutCartTotal'] = $cartTotal;
$cartOutput .= '<tr>
<td colspan="3" align="right" height="40px">Have you modified your basket? Please click here to <input class="btn_upd" type="submit" name="btnUpdate" value="Update" />&nbsp;&nbsp;</td>
<td align="right" style="background:#ccc; font-weight:bold"> Total: </td>
<td colspan="2" align="left" style="background:#ccc; font-weight:bold;">R '.$cartTotal.' </td>
<td style="background:#ccc; font-weight:bold"> </td>
</tr>
</table>
<div style="float:right; width: 215px; margin-top: 20px;">

<div class="checkout"><a href="checkout.php" class="more">Proceed to Checkout</a></div>

</div></form>';
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Great Selling Book Store</title>
<link href="css/slider.css" rel="stylesheet" type="text/css" />

<link rel="stylesheet" type="text/css" href="css/ddsmoothmenu.css" />

<link rel="stylesheet" type="text/css" href="css/styles.css" />

<script language="javascript" type="text/javascript">

function clearText(field)
{
if (field.defaultValue == field.value) field.value = '';
else if (field.value == '') field.value = field.defaultValue;
}
</script>

</head>

<body id="subpage">

<div id="main_wrapper">
<div id="main_header">
<div id="site_title"><h1><a href="#" rel="nofollow">Great Selling book Store</a></h1></div>

<div id="header_right">
<div id="main_search">
<form action="products.php" method="get" name="search_form">
<input type="text" value="Search" name="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field" />
<input type="submit" name="Search" value="" alt="Search" id="searchbutton" title="Search" class="sub_btn" />
</form>
</div>
</div> <!-- END -->
</div> <!-- END of header -->

<div id="main_menu" class="ddsmoothmenu">
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="products.php">Books</a></li>
<li><a class="selected" href="shoppingcart.php">Cart</a></li>
<li><a href="checkout.php">Checkout</a></li>
<li><a href="about.php">About</a></li>
</ul>
<br style="clear: left" />
</div> <!-- end of menu -->

<div class="cleaner h20"></div>
<div id="main_top"></div>
<div id="main">

<div id="sidebar">
<h3>Categories</h3>
<ul class="sidebar_menu">
<li><a href="index.php?cat=children">Children</a></li>
<li><a href="index.php?cat=Horror">Horror</a></li>
<li><a href="index.php?cat=Thriller">Thriller</a></li>
</ul>
</div> <!-- END of sidebar -->

<div id="content">
<?php echo $cartTitle; ?>
<?php echo $cartOutput; ?>

</div> <!-- end of content -->
<div class="cleaner"></div>
</div> <!-- END of main -->

<div id="main_footer">
<div class="cleaner h40"></div>
<center>
Copyright © 2048 DigitalNinja
</center>
</div> <!-- END of footer -->

</div>


<script type='text/javascript' src='js/logging.js'></script>
</body>
</html>

这是我在数据库中使用的表:

    CREATE TABLE IF NOT EXISTS `tblproduct` (
`prod_no` int(10) NOT NULL AUTO_INCREMENT,
`prod_id` int(15) NOT NULL,
`prod_name` varchar(100) NOT NULL,
`prod_descr` text NOT NULL,
`prod_cat` varchar(100) NOT NULL,
`prod_price` float NOT NULL,
`prod_quan` int(10) NOT NULL,
`date_added` datetime NOT NULL,
`ISBN` varchar(100) NOT NULL,
PRIMARY KEY (`prod_no`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;



INSERT INTO `tblproduct` (`prod_no`, `prod_id`, `prod_name`, `prod_descr`, `prod_cat`, `prod_price`, `prod_quan`, `date_added`, `ISBN`) VALUES
(1, 1, 'Charlie and the chocolate factory', 'prod description', 'Children', 80, 100, '2016-11-01 08:25:36', '9785811243570'),
(2, 2, 'Frankenstein', 'Prod description', 'Horror', 120, 80, '2017-05-01 05:27:11', '9781608438037'),
(3, 3, 'The Girl on the Train', 'Prod Description', 'Thriller', 200, 90, '2017-01-18 04:22:22', '9784062932530');

最佳答案

买家下订单后,您需要在数据库中输入:

UPDATE `tblproduct` SET prod_quan=prod_quan-$order_count WHERE prod_id=$product_id;

其中 $order_count 是订购的数量,$product_id - 订购的产品。为每个订购的产品执行此更新。

关于php - 减少 MySQL 购物车中的商品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43853842/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com