My db table looks like this pic. http://prntscr.com/22z1n
Recently I’ve created delete.php page. it works properly but when i deleted 21th user next registered user gets 24th id instead of 21.
Is it possible to put newly registered users info to first empty row? (In this situation 21th row)
In my registration form, newly registering user can write names of existing users, and be friends with them after registration. For this friendship i have another table that associates id of newly registered user and existing user.
For this purpose i’m using mysql_insert_id during registration to get id for new user. But after deletion of 21th row during nex registration process mysql_insert_id gave me number 21. but stored in 24th row. And put to associations table 21 for new user. I wanna solve this problem
A MySQL
auto_incrementcolumn maintains a number internally, and will always increment it, even after deletions. If you need to fill in an empty space, you have to handle it yourself in PHP, rather than use theauto_incrementkeyword in the table definition.Rolling back to fill in empty row ids can cause all sorts of difficulty if you have foreign key relationships to maintain, and it really isn’t advised.
The
auto_incrementcan be reset using a SQL statement, but this is not advised because it will cause duplicate key errors.EDIT
To enforce your foreign key relationships as described in the comments, you should add to your table definition:
Fill in the correct table and column names. Now, when a user is deleted from the registration, their friend association is nulled. If you need to reassociate with a different user, that has to be handled with PHP.
mysql_insert_id()is no longer helpful.If you need to find the highest numbered id still in the database after deletion to associate with friends, use the following.