gpt4 book ai didi

php - 如何在一个查询中更新来自不同表的两条记录?

转载 作者:行者123 更新时间:2023-11-29 01:55:36 25 4
gpt4 key购买 nike

我正在为学校构建我的电子商务项目,但我遇到了一个问题。如何自动减少库存并一次查询更改订单状态?

这是我的数据库:

table orders: id | customer_id | date | status | total

table orderitems: id | order_id | product_id | quantity

table products: id | category | name | description | image | price | stock

下面是从订单表中查看记录的代码:

<?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”,但我想在管理员单击“确认付款”时减少表产品中的库存 - 表订单项中的数量。

最佳答案

您可以按照下面的示例代码进行操作。

假设有两个表。

表1

create table db1 (
username varchar(10),
user_level integer
);

表2

create table db2 (
username varchar(10),
user_level integer
);

对于示例数据,插入几 strip 值的记录。

insert into db1 values ('a', 0), ('b', 2), ('c', 3); 
insert into db2 values ('a', 2), ('b', 1), ('c', 3);

这是您要求的单个查询,作为下面的示例

update db1 inner join db2 on db1.username = db2.username 
set db1.user_level = 6,
db2.user_level = 5
where db1.username = 'a';

关于php - 如何在一个查询中更新来自不同表的两条记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006227/

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