简体   繁体   English

在更新MySql,PHP上将值从一个表插入到另一个表

[英]Insert Value From One Table to Another on Update MySql, PHP

I was wondering if there was a way to take one value from a column in a Table and add it to a column in another table. 我想知道是否有一种方法可以从表的列中获取一个值并将其添加到另一表的列中。

The Scenario: I have a table (Shopping_Cart). 方案:我有一张桌子(Shopping_Cart)。 It stores all of my customers shopping cart items in the columns CustomerID , PID , Price , Status (Status can be "IN_CART" which means it was added and "OPEN" means that it has been paid for and ready to be processed and shipped). 它将所有我的客户购物车项目存储在CustomerIDPIDPriceStatus (状态可以是“ IN_CART”,表示已添加,而“ OPEN”表示已付款,可以进行处理和运输) 。 Then I have a table (Products). 然后,我有一张桌子(产品)。 It stores product information in columns PID , Price , Weight`. 它将产品信息存储在PID ,Price , Weight列中。 When My customers place an item in the shopping cart I do not capture the price. 当我的顾客将商品放入购物车时,我不会记录价格。 Prices can change daily so if they add something today, when they come back tomorrow the price may increase or decrease in their shopping cart. 价格每天都会变化,因此,如果今天增加价格,明天回来时价格可能会上涨或下降。 When my customer checks out and payment is confirmed, I am trying to grab the Price in the (Products) table where Products.PID = Shopping_Cart.PID and then apply that price to the Shopping_Cart.Price to lock in that final price since the customer has paid for it. 当我的客户结帐并确认付款后,我试图在(Products)表中获取“ Products.PID = Shopping_Cart.PID”中的价格,然后将该价格应用于Shopping_Cart.Price以锁定该最终价格,因为该客户已经付款了。 Any Ideas? 有任何想法吗? I got it set up to change the items for that customers cart to "OPEN" but I am a little lost on getting my script to grab a value from one table and applying it to another table. 我设置好了该客户购物车的项目,将其更改为“ OPEN”,但是让我的脚本从一个表中获取值并将其应用于另一个表时,我有些失落。 Below is my PHP Script for changing the Lines from "IN_CART" to "OPEN". 下面是我的PHP脚本,用于将行从“ IN_CART”更改为“ OPEN”。

if($The_Function=="CLOSE_LINES"){

    $cart_close_session_id = $_GET['close_session_id'];

    $response = array();


    require_once __DIR__ . '/db_connect.php';

    //connect to my db
    $db = new DB_CONNECT();

    $result = mysql_query("UPDATE shopping_cart SET Status = 'OPEN' WHERE SessionID LIKE '$cart_close_session_id' AND Status LIKE 'IN_CART'");

    if ($result) {
        $response["close_success"] = 1;

//GRAB PRICE FROM "PRODUCTS TABLE" AND UPDATE SHOPPING_CART 

    }else{
        $response["close_success"] = 0;
    }
    echo json_encode($response);
}

Assuming that you have a *shopping_cart* table with a price and a product_id column, you can update it with the price of the products table, like this: 假设您有一个带有价格product_id列的* shopping_cart *表,则可以使用products表的价格进行更新,如下所示:

UPDATE shopping_cart SET price = 
    (SELECT price FROM products WHERE id = shopping_cart.product_id)

You can join against the products table and update the shopping_cart upon purchase as seen below: 您可以加入产品表,并在购买时更新shopping_cart,如下所示:

UPDATE shopping_cart c
  JOIN products p 
    ON p.pid = c.pid
   SET c.price = p.price
 WHERE c.customer_id = ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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