I have the below queries which I'm iserting separately.
SQL1:
INSERT INTO tableA (product) VALUES ('machine1'), ('machine2')
SQL2:
INSERT INTO tableB (userid, name) VALUES (12345, 'Jan Smith'), (12345, 'Bert Smith'), (12346, 'Pieter King'), (12346, 'Martin King')
tableA has a unique primary key (autoincrement) on userID. tableB also has a column userID where userID is not unique.
Currently I use mysqli last inserted ID to obtain the first userID of the first inserted product from SQL1 which is machine1 in this case. In this case the number 12345 will be returned and will be used for SQL2.
I'm currently fetching the first userID and use it for the second SQL insertion while simply adding +1 for each next product assuming that the userID as given to the rows for SQL1 are all consecutive. As in the mean time there are already other insertions this approach is not valid and does not work. Further to that it is a dangeroes approach as the system breaks if any of the two SQL queries fail.
My question is: "How can I combine the above two INSERTION queries into 1?"