Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL stored prod insert from IN data and other table

I have an INSERT statement below, taking from 3 inputs that will be passed from a php post request. The statement below returns an error

INSERT INTO orderitems (orderId, productId, quantity, productName, price) (korderid, kproductid,kquantity, SELECT p.productName, p.price from products p WHERE p.id=kproductid)

The error being:

 The following query has failed: "CREATE PROCEDURE `CreateOrderItem`(IN `korderid` INT(8), IN `kproductid` INT(8), IN `kquantity` INT(8)) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER INSERT INTO orderitems (orderId, productId, quantity, productName, price) (korderid, kproductid,kquantity, SELECT p.productName, p.price from products p WHERE p.id=kproductid)" 

 MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'korderid, kproductid,kquantity, SELECT p.productName, p.price from products p...' at line 1 ``` 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You did not mention how you’re doing with the post data. But here is how it could be done:

INSERT INTO orderitems (orderId, productId, quantity, productName, price)
SELECT :orderid, p.id, :quantity, p.productName, p.price
FROM products p
WHERE p.id = :productid

The :xyz indicates parameters for the query.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading