SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Location
    md
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to select IDs from database

    I made an editor that can edit and delete records in the database. I also have a page that displays all records and links to the editor. I am using a count to set the IDs for each of the records, but when I delete a record, that ID is no longer there, so it is no accurate.. how would I go about doing this?

    Thanks!

  2. #2
    SitePoint Wizard Pedro Monteiro's Avatar
    Join Date
    Sep 2002
    Location
    Lisbon
    Posts
    1,393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    $query 
    "delete from table where id='$id'"

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Gexus
    I made an editor that can edit and delete records in the database. I also have a page that displays all records and links to the editor. I am using a count to set the IDs for each of the records, but when I delete a record, that ID is no longer there, so it is no accurate.. how would I go about doing this?

    Thanks!
    Perhaps you should be making your id an autoincrement ing field, so it keeps an internal pointer to the last id used. Read your documentation.

    Realisticly the id is only useful to you, the administrator, why would you worry about if you delete id #4 and its then gone?

    The default behaviour of autoincrement is to ignore deleted ids, perhaps this is causing your problem. You can recover the "lost" id - but you wouldnt really want that level of operator (website) to do that for you, I cant remember the command off hand.
    isempty()

  4. #4
    SitePoint Member tpra21's Avatar
    Join Date
    Feb 2006
    Location
    Columbus, Ohio
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use the same functionality in my site. Here is what you do:
    PHP Code:
    function update_ids(){
             global 
    $conn;
             
    $unit_number $_POST['unit_number'];
             
    $username $_SESSION['username'];
             
    $q "UPDATE unit_info set pu_unique_id = pu_unique_id - 1 where pu_username = '$username' and pu_unique_id > '$pu_number'";
             return 
    mysql_query($q,$conn); 

    What the above function does is everytime you delete a record, you are subtracting 1 from all IDs that are greater than the one (record) you are deleting. This keeps your IDs in order and unique.

    For example: You delete record 5. You would have 1,2,3,4,6 with your current code, thus making your IDs useless becuse they are now out-of-order. If you use the code I posted (manipulated for your use) you would have the following IDs(records) left after deleting record 5:

    1,2,3,4,5
    http://www.landlorddocuments.com
    More than 70 free real estate forms available.


Bookmarks

Posting Permissions

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