-4

I am working on a web application. It will have multiple users. I am using mysql as database.

In my application i am fetching the latest id (from the database, using max(id)) , and then generating the next id for the new registration. This approach is incorrect, as the id might change between the time i update an id

How should i proceed?

1 Answers1

-1

LAST_INSERT_ID() or AUTO_INCREMENT will be your friends

LAST_INSERT_ID() :

INSERT INTO table (id, field) VALUES (LAST_INSERT_ID() + 1, "Hello")

AUTO_INCREMENT :

INSERT INTO table (field) VALUES ("Hello")

And the id is auto incrementing

Some documentation here :

https://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

https://dev.mysql.com/doc/refman/5.0/fr/example-auto-increment.html

Betezed
  • 11
  • 2