dporb84480 2013-08-13 17:56
浏览 45
已采纳

在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). 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). Then I have a table (Products). It stores product information in columns 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. 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. Below is my PHP Script for changing the Lines from "IN_CART" to "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);
}
  • 写回答

2条回答 默认 最新

  • dti70601 2013-08-13 18:02
    关注

    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:

    UPDATE shopping_cart SET price = 
        (SELECT price FROM products WHERE id = shopping_cart.product_id)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置