【发布时间】:2020-01-16 20:21:32
【问题描述】:
我目前正在为大学项目开发电子商务网站,并且我正在尝试在下订单后减少库存。无法确定将引用产品表中“库存”行的代码行放在哪里。 我将在此处添加“action.php”代码。 很抱歉代码太长,不知道您需要什么帮助。
<?php
session_start();
require 'config.php';
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
$pname = $_POST['pname'];
$pprice = $_POST['pprice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = 1;
$stmt = $conn->prepare("SELECT product_code FROM cart WHERE product_code=?");
$stmt->bind_param("s",$pcode);
$stmt->execute();
$res = $stmt->get_result();
$r = $res->fetch_assoc();
$code = $r['product_code'];
if(!$code){
$query = $conn->prepare("INSERT INTO cart (product_name,product_price,product_image,qty,total_price,product_code) VALUES (?,?,?,?,?,?)");
$query->bind_param("sssiss",$pname,$pprice,$pimage,$pqty,$pprice,$pcode);
$query->execute();
echo '<div class="alert alert-success alert-dismissible mt-2">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item added to your cart!</strong>
</div>';
}
else{
echo '<div class="alert alert-danger alert-dismissible mt-2">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Item already added to your cart!</strong>
</div>';
}
}
if(isset($_GET['cartItem']) && isset($_GET['cartItem']) == 'cart_item'){
$stmt = $conn->prepare("SELECT * FROM cart");
$stmt->execute();
$stmt->store_result();
$rows = $stmt->num_rows;
echo $rows;
}
if(isset($_GET['remove'])){
$id = $_GET['remove'];
$stmt = $conn->prepare("DELETE FROM cart WHERE id=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'Item removed from the cart!';
header('location:cart.php');
}
if(isset($_GET['clear'])){
$stmt = $conn->prepare("DELETE FROM cart");
$stmt->execute();
$_SESSION['showAlert'] = 'block';
$_SESSION['message'] = 'All Item removed from the cart!';
header('location:cart.php');
}
if(isset($_POST['qty'])){
$qty = $_POST['qty'];
$pid = $_POST['pid'];
$pprice = $_POST['pprice'];
$tprice = $qty*$pprice;
$stmt = $conn->prepare("UPDATE cart SET qty=?, total_price=? WHERE id=?");
$stmt->bind_param("isi",$qty,$tprice,$pid);
$stmt->execute();
}
这里有“cart.php”代码,如果你还需要什么,请告诉我。
<?php
require 'config.php';
$stmt = $conn->prepare("SELECT * FROM cart");
$stmt->execute();
$result = $stmt->get_result();
$grand_total = 0;
while($row = $result->fetch_assoc()):
?>
<tr>
<td><?= $row['id'] ?></td>
<input type="hidden" class="pid" value="<?= $row['id'] ?>">
<td><img src="<?= $row['product_image'] ?>" width="50"></td>
<td><?= $row['product_name'] ?></td>
<td>
<i class="fas fa-dollar-sign"></i> <?= number_format($row['product_price'],2); ?>
</td>
<input type="hidden" class="pprice" value="<?= $row['product_price'] ?>">
<td>
<input type="number" class="form-control itemQty" value="<?= $row['qty'] ?>" style="width:75px;">
</td>
<td><i class="fas fa-dollar-sign"></i> <?= number_format($row['total_price'],2); ?></td>
<td>
<a href="action.php?remove=<?= $row['id'] ?>" class="text-danger lead" onclick="return confirm('Are you sure want to remove this item?');"><i class="fas fa-trash-alt"></i></a>
</td>
</tr>
<?php $grand_total +=$row['total_price']; ?>
<?php endwhile; ?>
【问题讨论】:
-
PHPMyAdmin 不是数据库。它是 MySQL 数据库的 Web 界面。
-
是的,对不起..我的错。
-
我改了标题,你能看一下代码吗?
-
我只看到处理购物车的代码。库存是什么意思?
-
这里有你需要的所有代码:action.php:pastebin.com/4naXb14Sindex.php:pastebin.com/zVnqcWutcart.php:pastebin.com/SYHJVVgL
标签: php mysqli shopping-cart