O
O
ortsuev332018-08-09 09:51:01
SQL
ortsuev33, 2018-08-09 09:51:01

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

6 answer(s)
P
poniyur, 2018-08-09
@ortsuev33

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

L
Lander, 2018-08-09
@usdglander

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.

A
Alexander, 2018-08-09
@xpert13

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.

L
LobsterJoe, 2018-08-09
@LobsterJoe

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.

A
Alexander, 2018-08-09
Madzhugin @Suntechnic

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.

I
ixon, 2018-08-09
@ixon

Using SQL, query the maximum element of the table in the id field and add 1 to it. This will be the identifier of the future new record.
MAX(id)+1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question