[英]how to update two records from different table in one query?
I'm building my e-commerce project for school, but i'm facing a problem. 我正在为学校建立我的电子商务项目,但是我遇到了一个问题。 How to reduce stock automatically and change the order status in one query?
如何在一个查询中自动减少库存并更改订单状态?
here's my database : 这是我的数据库:
table orders: id |
表订单:id | customer_id |
customer_id | date |
日期| status |
状态| total
总
table orderitems: id |
表订单项:id | order_id |
order_id | product_id |
product_id | quantity
数量
table products: id |
餐桌产品:id | category |
类别| name |
名称| description |
描述| image |
图片 price |
价格| stock
股票
Here's the code for viewing records from table orders : 这是用于查看表订单记录的代码:
<?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>
i'm successful for updating status to '10' but i want to reduce stock by stock from table products - quantity from table orderitems when the administrator click the 'Confirm Payment'. 我已成功将状态更新为“ 10”,但我想减少表产品的库存量-当管理员单击“确认付款”时,表订单项的数量减少。
You can do it like the sample code below. 您可以像下面的示例代码那样进行操作。
Assume there are two tables. 假设有两个表。
Table 1 表格1
create table db1 (
username varchar(10),
user_level integer
);
Table 2 表2
create table db2 (
username varchar(10),
user_level integer
);
For sample data inserting few records with values. 对于样本数据,请插入一些带有值的记录。
insert into db1 values ('a', 0), ('b', 2), ('c', 3);
insert into db2 values ('a', 2), ('b', 1), ('c', 3);
Here is the single query which you asked for as sample below 这是您在以下示例中要求的单个查询
update db1 inner join db2 on db1.username = db2.username
set db1.user_level = 6,
db2.user_level = 5
where db1.username = 'a';
You can´t update different tables in the same query. 您不能在同一查询中更新不同的表。 If you want to do so and prevent an inconsistency in the db because of the 2nd update failing you should use transactions otherwise you should use 2 queries.
如果要这样做并防止由于第二次更新失败而导致数据库不一致,则应使用事务,否则应使用2个查询。 You can implement transactions in your code, you may implement a trigger on update or you can create a stored procedure in the DB.
您可以在代码中实现事务,可以在更新时实现触发器,也可以在数据库中创建存储过程。
Wish i made myself clear and it helps! 希望我能使自己清楚,这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.