Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts

    php -- inserting the missing ID problem

    hi friends


    I have tha database table the ID whic is the PK of the table
    Now when the users adds something this i use MAX(ID) to insert the next ID automatically

    But when sometimes the users deletes that quantity then thst IDs are missing and the ID always go on incrasing

    What i want is is it possible to first inser those missing IDs and then if all the missing IDs are full then use MAX(ID) and isert data

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What kind of database are you using? If its a mysql database why not use auto_increment?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    i am using mysql databse
    The auto_increment is also same as max(ID)
    it does not fill the missing colums

    like i have id 1 2,3,4,5
    if i delete id = 3 and insert next record then with auto_increment the next record will be added with ID 6 not at 3

  • #4
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That's how its supposed to work. Why do you need the IDs to be sequential?

    Dan
    Last edited by whizard; 07-28-2007 at 07:08 AM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #5
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    it looks good when the rows are squential otherwise the rows number will be very large after sometime
    I am thinking of one solution tell me if thats ok or not

    Will it be better that when i delete any rows i will store that ids in different table column and when i need to insert any data
    the i will select the min(ID) from that column name and if there is no ID in that column then i will use select MAX(id) from the current table

    Is this solution viable

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    It works in theory, but I would advise you against any such plan. There's no reason that IDs need to be sequential; you'll eventually end up with large ID#'s anyhow, and its not a problem. No one sees the database, so no one will know what it looks like

    My advice is don't worry about it - focus your energies on the actual coding of your site.



    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    New Coder thindrakhya's Avatar
    Join Date
    Jun 2007
    Posts
    90
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Thanks for your help whizard

    I want to ask one thing if you can guide

    In the PHPforums database desgin when all the registered users posts new topic or send reply then do all the posts from the all users store in same table with fields something like

    post_id , sender-name , .............

    or the posts from different users are stored in differnt tables

    I want to know because this way post_id coulmn will be very very bigger

  • #8
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    I don't know for sure, but I would imagine that all the posts are stored in one table... having a seperate table for each user would be dumb, in my opinion.

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #9
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Usually the reason the auto increment appends the record to the end of the table its to keep integrity. For instance if someone had an ID 12 then posted a comment before there account was deleted then what would happen if you filled the missing ID with a new instance. Then you would be saying the post was submitted by the wrong person. I usually always keep the data in the table even if i delete the users account then i would have a boolean field that i could set to 1 for active and 0 for inactive, so if a user leaves i can just set it to inactive
    You can not say you know how to do something, until you can teach it to someone else.

  • #10
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by thindrakhya View Post
    it looks good when the rows are squential otherwise the rows number will be very large after sometime
    I am thinking of one solution tell me if thats ok or not
    The size of an auto number ID is relatively small in comparison to say a large varchar or a floating point variable.
    You can not say you know how to do something, until you can teach it to someone else.

  • #11
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,026
    Thanks
    2
    Thanked 315 Times in 307 Posts
    I'll expand a little on something timgolding has stated. In real applications, database records are not really deleted. They are just marked as being deleted/inactive and normal queries don't include them. Even for something like a Forum, "deleted" records are just marked as such and still exist so that they can be retrieved if needed or have reports run on them...
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #12
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Yes and even if you do decide it is OK to remove them then in most cases you should still use a unique ID
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •