Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Select inside insert operation fails.

    I need to make a SELECT inside an INSERT operation, but it fails everytime with this error message:

    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT original_price FROM oc_original_prices WHERE product_id = '12'


    This is my php coding:

    PHP Code:
     $this->db->query("INSERT INTO " DB_PREFIX "order_product SET order_id = '" . (int)$order_id "', product_id = '" . (int)$product['product_id'] . "', original_price = SELECT original_price FROM ".DB_PREFIX."original_prices WHERE product_id = '" .(int)$product['product_id']."'"); 
    Which results into this sql code:

    Code:
    INSERT INTO oc_order_product SET order_id = '25', product_id = '12', original_price = SELECT original_price FROM oc_original_prices WHERE product_id = '12'
    I hope someone can tell me what I need to alter to make the sql work.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,553
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Wrong way, entirely.

    Like this:
    Code:
    INSERT INTO oc_order_product (order_id, product_id, original_price )
    SELECT 25, product_id, original_price FROM oc_original_prices WHERE product_id = 12;
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •