Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在一个查询中更新来自不同表的两条记录?_Php_Mysql_Localhost - Fatal编程技术网

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

Php 如何在一个查询中更新来自不同表的两条记录?,php,mysql,localhost,Php,Mysql,Localhost,我正在为学校建设我的电子商务项目,但我面临一个问题。如何在一次查询中自动减少库存并更改订单状态 这是我的数据库: 表顺序: id| 客户识别码 日期| 地位| 总数 表orderitems: id| 订单号 产品标识 数量 表产品: id| 类别| 名称| 描述| 图像| 价格| 股票 以下是查看表订单记录的代码: <?php session_start(); ob_start(); if(ISSET($_SESSION['SESS_ADMINLOGGEDIN'])){ }

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

这是我的数据库:

表顺序: id| 客户识别码 日期| 地位| 总数

表orderitems: 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”,但当管理员单击“确认付款”时,我想从表products-表orderitems中逐个减少库存。您不能在同一查询中更新不同的表。如果要这样做并防止由于第二次更新失败而导致数据库不一致,则应使用事务,否则应使用2个查询。您可以在代码中实现事务,可以在更新时实现触发器,也可以在数据库中创建存储过程


希望我能说清楚,这会有帮助

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

假设有两张桌子

表1

create table db1 (
username varchar(10),
user_level integer
);
表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';

为什么必须是一个查询?使用一些查询并将它们放入“事务”中。例子