简体   繁体   中英

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 | customer_id | date | status | total

table orderitems: id | order_id | product_id | quantity

table products: 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'.

You can do it like the sample code below.

Assume there are two tables.

Table 1

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

Table 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. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM