I’ll try and make this question as brief as possible. I was wondering if there is a way to do the following via PHP & MySQL coding:
-
Delete an auto-incremented id entry (say entry 12 of 20), and at the same time lower the rest of the entries by 1. (Example: Delete entry 12 == Entry 13 becomes 12, 14 becomes 13, etc. etc.)
-
Manually (i.e via form) insert an entry with an ID so that the rest are pushed up if specified by the user (and defaults to normal behaviour if nothing is specified. (Example: User specifies that he wants his entry to be ID 14, but there is already an ID 14 in the database. What should happen would be that the user input pushes the already existing entry up to 15, & the rest of the existing entries follow a similar fashion. If the user does not specify an ID, default to normal behaviour.)
I’m still learning MySQL & PHP and the code I need is for an image gallery system that I’ve been building for a while now.
Is it at all possible?
For task 1, you would need to manually set the AUTO_INCREMENT value for that table after modifying all of the IDs so there’s no gap when the next insert happens. A far better way is to not use an auto-increment ID at all – have an ID column, and use MAX(id)+1 to find the required ID at the time of record insertion.
here’s my psuedocode:
for a deletion:
for a specified insertion:
for an unspecified insertion