Hello and welcome to our community! Is this your first visit?
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
    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:

    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. #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    Wrong way, entirely.

    Like this:
    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;
    Be yourself. No one else is as qualified.


Posting Permissions

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