SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preventing double entry in mySQL database

    I have made an insert PHP page for inserting a city into my database into the table tblPlace:
    tblPlace
    placeID
    placeName
    stateID

    How can I prevent that someone inserts e.g. Amsterdam twice in the table ?

    Greetz Brain

  2. #2
    epsilon transition cupid's Avatar
    Join Date
    Aug 2001
    Location
    Kent, Ohio
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm.. perhaps some clarifications would help us in coming up with a solution.

    I'd imagine that placeID is the primary key.. what is StateID? Also, is there a list where users pick these locations or are they simply entering them?

    If they're entering them instead of selecting these from a list.. I don't see an easier way to do this other than to query tblPlace and match each placeName with what they entered to see if it's already in it.

  3. #3
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can create a unique index on placeName. That should break when you try and insert dupe values (never tried it in MySQL).

  4. #4
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OK, I'm going to try it all

    Thanks for the quick reply

  5. #5
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i've used the unique field when making a table.
    seems to work pretty good. Should shoot a warning back if it exists already.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  6. #6
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to make my own error page

    Unique fields helps and it works. But how can I make my own error page in stead of:

    Duplicate entry 'amsterdam' for key 2

    Greetz Brain

  7. #7
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if (mysql_error()) {
      if (
    strstr(mysql_error(),"Duplicate entry") {
        
    // custom error message/include page here
      
    } else {
        
    // echo normal error message? or do whatever you want
        
    echo mysql_error();
      }

    Kevin

  8. #8
    SitePoint Addict brain's Avatar
    Join Date
    Sep 2001
    Location
    Amsterdam
    Posts
    249
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    tanx

    OK, that helps, thank you !


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
  •