duanqian6982 2018-06-02 15:53
浏览 129

如何在php中准备INSERT INTO SELECT查询?

I'm trying to copy products from a cart (from cart_product) to the order_product table

Can this query work this way by preparing the the column name to become a value of the order_id

    // Create the order

    $order_id = $this->order->store();

    // Copy products from cart and assign them to the order

    $req = "INSERT INTO order_product (order_id, product_id)
    SELECT :order_id, product_id, 
    FROM cart_product, cart
    WHERE cart.user_id = :id";

    $bind = array(
        "id" => $_SESSION['id'],
        "order_id" => $order_id
    );

    return  $this->Sql($req);

I'm using a micro framework, where the Sql function came.

I want the query to become something like this

INSERT INTO order_product (order_id, product_id)
SELECT 3, product_id, 
FROM cart_product, cart
WHERE cart.user_id = 2
  • 写回答

1条回答 默认 最新

  • duancan9815 2018-06-02 16:32
    关注

    Solution : The problem was caused by the comma at the end of the FROM line. That is what was causing the problem and not the inability to bind columns as values. I had no error messages because I'm using a micro proprietary framework.

    评论

报告相同问题?

悬赏问题

  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)