SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert into database between a set number?

    Hello,

    I have a very quick question. Say I have a database like so:

    id | name
    1 | John
    2 | Mark
    3 | Joe
    4 | Matt
    5 | Ruth
    101 | Matt
    102| Jeff
    103| Mark
    104| Lydia
    105| Jeff
    201| Pike
    202 | Niall
    204| Webb
    205 | Truth

    When it comes to inseting a record into the database:

    PHP Code:
          $result mysql_query("Insert into tbl (name)  
                       values ('
    $name')") or die(mysql_error()); 
    How can I force my query to insert a row in the database at the next free number between 106 and 200 for instance as normally it would just check the new record on mysql after 205 as 206?

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7724 View Post
    Hello,

    I have a very quick question. Say I have a database like so:

    id | name
    1 | John
    2 | Mark
    3 | Joe
    4 | Matt
    5 | Ruth
    101 | Matt
    102| Jeff
    103| Mark
    104| Lydia
    105| Jeff
    201| Pike
    202 | Niall
    204| Webb
    205 | Truth

    When it comes to inseting a record into the database:

    PHP Code:
          $result mysql_query("Insert into tbl (name)  
                       values ('
    $name')") or die(mysql_error()); 
    How can I force my query to insert a row in the database at the next free number between 106 and 200 for instance as normally it would just check the new record on mysql after 205 as 206?

    Thanks
    If you want to manage the identifier yourself, you can disable auto_increment.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7724, there are 2 cases.
    1. you don't need id field as unique identifier. so, get rid of it and call it something different. say, "number"
    2. you need unique identifier. then don't touch it ever.

  4. #4
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys,

    Have a bit of a problem with this not being my database so id is set as a primary key and auto increments.

    This would then cause a problem I guess. Is there any other way around it?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can alter the table to change the next auto increment, or perform selects to find avail numbers in a sequence. But as mentioned, don't touch it, it is irrelevant aside from being a unique identifier.

  6. #6
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7724 just tell us reason, why do you want such unusual thing
    and real table structure please


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
  •