0

Whenever a user creates an account, I want their ID to increase by 1, so we have something like this:

User 1 >> ID
User 2 >> ID 1 more than previous.
User 3 >> ID 1 more than previous.
User 4 >> ID 1 more than previous.

So how can I make this in PHP and a MySQL database?

Dharman
  • 30,962
  • 25
  • 85
  • 135
H Bellamy
  • 22,405
  • 23
  • 76
  • 114

3 Answers3

1

On database side create a table to save the users with a field id as integer

PRIMARY KEY, AUTOINCREMENT

and if you want you can also define it as UNSIGNED (this way the field can be only positive)

this way it will be done automatically when you insert a row into the database...

Example Query Insert:

INSERT INTO table (id,username,password) 
 values (,'UsernameSelected','PasswordSelected');
Marcx
  • 6,806
  • 5
  • 46
  • 69
0

Set the Id field as auto_increment. You can follow this tutorial to set it using phpmyadmin: tutorial

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
0

Keep in mind what will happen if a user is deleted. If your goal is to always have user001 through user100 to be filled, I don't believe incrementing by 1 alone won't solve your issue. You'll have to do a check to see what "spots are open" between the lowest number and highest number. If there's one or more, those will have to be filled first and only when there are none, would you take the highest number and continue adding 1.

Just a thought.