如何在一次查询中更新来自不同 table 的两条记录?

how to update two records from different table in one query?

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

这是我的数据库:

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

这是查看来自 table 个订单的记录的代码:

<?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”,但我想减少 table 产品中的库存 - 当管理员单击 'Confirm Payment' 时,table 订单项的数量.

您不能在同一个查询中更新不同的表。如果你想这样做并防止由于第二次更新失败而导致数据库不一致,你应该使用事务,否则你应该使用 2 个查询。您可以在代码中实现事务,可以在更新时实现触发器,也可以在数据库中创建存储过程。

希望我把自己说清楚,这很有帮助!

您可以像下面的示例代码那样做。

假设有两个表。

Table 1

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

Table 2

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

对于样本数据插入几条带值的记录。

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';