Answer the question
In order to leave comments, you need to log in
How to get the id of the future record?
There is a product_name, you need to get the id that this entry will have in the database when inserted, i.e. on the site I will have a line of product_id name, then I will break the line and get the product by id. I need, so to speak, the future id of this record, and even purely theoretically, if the request would last 1 minute, how would the id of the Autoincr records be distributed, both records would have the same id or immediately formed in turn, the question is what could be when one insert failed and the second one is coming
Answer the question
In order to leave comments, you need to log in
If the insert is long, then just insert with empty data first.
Then the request will pass quickly, id will be. Next, use update.
There is an example on PDO how to get the last inserted id of the record you will update
To get the next value of an auto-incrementing field, you can use the query:
But as my colleague Alexander rightly noted , the idea is absolutely idiotic.
It's not normal for you to get the id of the future entry. If you need to use an id, then first an insert is made, an id is obtained, and only then editing with adding this id where you need it.
Stop using auto-increment Id.
I would use a call to this library in the object's constructor to generate the Id. The probability of collisions is so small that it can be neglected.
Although the idea is crazy in itself, but I would decide this way - I would make a record in the database where all fields are empty, take its id and use it, and when in the future it would be necessary to add a product with this future id, I would update this record.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question