I am trying to add a record in a table through a php form. One of the required fields needs to be selected from another table and added into the insert statement before execution.
Here is the mysql code, this works perfectly fine in Workbench by getting the 'Select' part and including it the 'Insert' all at the same time. My php form returns an error saying that the 'serial' value is null so the 'ADD' fails.
Here is the working mysql -
INSERT INTO table_1
(type, version, regn, comments, serial)
VALUES ('797', '109F','KILO','new entry', (SELECT serial
FROM table_2 WHERE consno = '999999'))
As someone who is very new to this I recall reading something along the lines of multiple statements not being possible from within php, any advice or direction on how I can get this 'Insert' to work within php would be much appreciated?
Thanks
You're doing it wrong. You need the INSERT ... SELECT
pattern.
INSERT INTO table_1
(type, version, regn, comments, serial)
SELECT '797', '109F','KILO','new entry', serial
FROM table_2
WHERE consno = '999999'
You write a SELECT statement matching the columns you want to insert. Then you put your INSERT statement right before it. The trick here is to specify constants when you need them, and columns from the table where those are needed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.