I can somewhat see what you want here. You want to make sure that you have multiple inserts and somehow ensure that they have some sort of consistent ID associated with each transaction. If you take the timestamp approach, there's no guarantee that they will all be inserted in the same second.
It's very difficult to have rows inserted simultaneously in MySQL but I do have a suggestion that might work. I'm thinking that you should start a transaction, find the next highest ID number, and insert all of those rows afterwards.
$transRes = mysql_query("START TRANSACTION");
$nextListRes = mysql_query("SELECT max(listNumber) FROM InventoryRequests");
- Fetch the result from
$nextListRes
and add 1.
- Run
INSERT
queries for the SKUs using this incremented value.
$commitRes = mysql_query("COMMIT");
You want to make sure that this is a transaction and not two separate queries because race conditions may exist where two instances of your running PHP code may SELECT
at the same time (a very bad thing!).
Also, I was thinking of a subquery way to do it, but that would make it such that each INSERT
statement run will have a different MAX(listNumber)
each time it's executed, meaning that they won't have the same ID as you would like. You might want to look into prepared statements for the INSERT
s if you need them to be a bit faster, too :)