如何将记录的每一行插入数据库的表“ receipt”中?
每行购买记录作为图片。
这是我最近实现的代码
if (isset($_POST["add"])){
if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
}
$_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
$_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = $_SESSION["cart"][$_GET["id"]]['item_quantity'] + $_POST["quantity"];
header('Location: counter.php');
}
if (isset($_GET["action"])){
if ($_GET["action"] == "delete"){
foreach ($_SESSION["cart"] as $keys => $value){
if ($value["id"] == $_GET["id"]){
unset($_SESSION["cart"][$keys]);
echo '<script>alert("Product has been Removed")</script>';
echo '<script>window.location="counter.php"</script>';
}
}
}
}
if (isset($_POST["minus"])){
if(!isset($_SESSION["cart"][$_GET["id"]]['item_quantity'])){
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = 0;
}
$_SESSION["cart"][$_GET["id"]]['item_name'] = $_POST["hidden_name"];
$_SESSION["cart"][$_GET["id"]]['product_price'] = $_POST["hidden_price"];
$_SESSION["cart"][$_GET["id"]]['item_quantity'] = ( ( ( $_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] ) > 1 ) ?
$_SESSION["cart"][$_GET["id"]]['item_quantity'] - $_POST["quantity"] : 1 );
header('Location: counter.php');
}
<?php
$total_price = 0;
$change = 0;
$selectOption = 0;
if(!empty($_SESSION["cart"])){
foreach ($_SESSION["cart"] as $key => $value) {
?>
<tr>
<td style="background-color:#FFB6C1"; width="7%"><?php echo $value["item_name"]; ?></td>
<td style="text-align:center;background-color:#E0FFFF"; width="1%"><?php echo $value["item_quantity"]; ?></td>
<td style="text-align:center;background-color:#D8BFD8"; width="3%">RM <?php echo number_format($value["item_quantity"] * $value["product_price"], 2); ?></td>
<td style="text-align:center;background-color:#FAFAD2"; width="3%"><a href="counter.php?action=delete&id=<?php echo $value["id"]; ?>">
<img src="img/icon-delete.png"/></a></td>
</tr>
<?php
$total_price = $total_price + ($value["item_quantity"] * $value["product_price"]);
?>
<!--for onkeyup-->
<script>
var total = '<?php echo $total_price ;?>';
</script>
<?php
if(isset($_POST["cash"])){
$cash = $_POST['cash'];
$change = $_POST["cash"] - $total_price;
$_SESSION["cash"] = $_POST["cash"];
}
else {
$change = "0";
$_SESSION["cash"] = 0;
}
}
}
?>
</table>
</div>
<div style="border:0px solid black; width:400px">
<table class="table table-bordered table-striped" width="200px">
<form method="POST" action="receipt.php">
<tr>
<th colspan="2" style="text-align:right">Total:</th>
<th style="text-align:center;color:white;background-color:#9932CC" id="total">RM <?php echo number_format($total_price,2); $_SESSION["total"] = $total_price;?></th>
</tr>
<tr>
<th colspan="2" style="text-align:right">Cash:</th>
<th><center>
<input type="number" id="myInput" style="width:90px;padding:0;" onkeyup="myFunction()" name="cash" step=".01">
</center>
</th>
</tr>
<th colspan="2" style="text-align:right;">Change:</th>
<th style="text-align:center;width:90px;color:white;background-color:#00FF00" id="change">RM <?php echo number_format($change,2); ?></th>
</tr>
</table>
</div>
<div style="border:0px solid black; width:400px">
<table class="table table-bordered table-striped" style="margin-top:-30px">
<tr>
<th style="text-align:center">
<input type="hidden" name="action" value="submit"></input></td>
<input type="submit" name="payment" value="Payment" id="submit_purchase" class="btn btn-warning btn-xs">
</form>
</th></tr>
</table>
</div></div>
</div>
<script>
function myFunction() {
var x = document.getElementById("myInput").value;
var y = total;
var z = x - y;
var z = z.toFixed(2);
document.getElementById("change").innerHTML = z;
}
</script>
//receipt.php
session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == 'submit')
{
if(isset($_SESSION["cart"]))
{
$link=mysqli_connect("localhost","root","","testing");
foreach($_SESSION["cart"] as $keys => $values)
{
$name =$values["product_name"];
$quantity=$values["product_quantity"];
$price =$values["product_price"];
$subprice=$values["product_quantity"] * $values["product_price"];
date_default_timezone_set("Asia/Kuala_Lumpur");
$date = date("Y-m-d");
$total_price = $total_price + ($values["product_quantity"] * $values["product_price"]);
$total_item = $total_item + 1;
$query="INSERT INTO receipt(purchase_name, product_quantity, price, subprice, total, date)
VALUES('$name','$quantity','$price','$subprice', '$total_price', '$date')";
if(mysqli_query($link,$query)){
print "<p>The entry has been added!</p>";
}else{
print '<p style="color:red;">Could not add the entry because:<br>' . mysqli_error($link) .
'.</p><p>The query being run was: ' . $query .' </p>';
}
}
mysqli_close($link);}}}
if(isset($_SESSION["cart"])){
unset($_SESSION['cart']);
}
?>
在点击付款后将所有交易存储到收据表中,属性诸如
收据
- receipt_id
- 购买名称
- product_quantity
- 价格
- 价格
- 总计
- 日期