我正在爲學校建立我的電子商務項目,但我面臨一個問題。如何自動減少庫存並更改一個查詢中的訂單狀態?如何在一個查詢中更新不同表中的兩條記錄?
這裏是我的數據庫:
訂單表: ID | customer_id | date | 狀態| 全部
表orderitems: ID | order_id | product_id | 數量
桌面產品: ID | 類別| 名稱| 描述| 圖片| 價格| 股票
下面是從訂單表查看記錄代碼:
<?php session_start();
ob_start();
if(ISSET($_SESSION['SESS_ADMINLOGGEDIN'])){
}
else{
header("location: login.php");
}
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"/ >
<link rel="stylesheet" href="css/style_admin.css" type="text/css" />
<title>Untitled Document</title>
</head>
<body>
<?php
require("../config.php");
require("../functions.php");
if(!isset($_SESSION['SESS_ADMINLOGGEDIN'])) {
header("Location: " . $config_basedir);
}
if(isset($_GET['func']) == TRUE) {
$funcsql = "UPDATE orders SET status = 10 WHERE id = " . $_GET['id'];
mysql_query($funcsql);
header("Location: orders.php");
}
else {
require("header.php");
echo "<h1>Outstanding orders</h1>";
$orderssql = "SELECT * FROM orders WHERE status = 2";
$ordersres = mysql_query($orderssql);
$numrows = mysql_num_rows($ordersres);
if($numrows == 0)
{
echo "<strong>No orders</strong>";
}
else
{
echo "<table cellspacing=10>";
while($row = mysql_fetch_assoc($ordersres))
{
echo "<tr>";
echo "<td>[<a href='adminorderdetails.php?id=" . $row['id']. "'>View</ a>]</td>";
echo "<td>". date("D jS F Y g.iA", strtotime($row['date'])). "</td>";
echo "<td>";
if($row['registered'] == 1)
{
echo "Registered Customer";
}
else
{
echo "Non-Registered Customer";
}
echo "</td>";
echo "<td>Rp. ;" . sprintf('%.2f',
$row['total']) . "</td>";
echo "<td>";
if($row['payment_type'] == 1)
{
echo "PayPal";
}
else
{
echo "Cheque";
}
echo "</td>";
echo "<td><a href='orders.php?func=conf&id=" . $row['id']. "'>Confirm Payment</a></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
</body>
</html>
我是成功的更新狀態爲「10」,但我想通過股票從表產品,以減少庫存 - 從數量當管理員點擊'確認付款'時,該表格的訂單項目。
作爲樣品爲什麼它必須是一個查詢?使用幾個查詢並將它們放入「事務」中。示例[在本教程中,您將學習MySQL事務以及如何使用MySQL COMMIT語句和MySQL ROLLBACK狀態](http://www.mysqltutorial.org/mysql-transaction.aspx) –