Handling duplicate entry errors?

Discussion in 'Web Design and Development' started by MythicFrost, Apr 5, 2010.

  1. MythicFrost macrumors 68040

    MythicFrost

    Joined:
    Mar 11, 2009
    Location:
    Australia
    #1
    Hi,

    I've got a DB set up with an e-mail and user column that's unique.
    That way if anyone enters a username or e-mail identical to someone else I can handle it and say "that username or email is already taken".

    At the moment I'm just getting the output (error) from this code:
    PHP:
    if (!mysql_query($sql,$con)) {
        die(
    'Error: ' mysql_error());
    }
    which is: Error: Duplicate entry 'myusername-myusername@anywhere.com' for key 'user'
    Any thoughts on how to do this?

    Kind Regards
     
  2. Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #2
    Ok, not sure if I understand the premise of the question, but I'm assuming that you want to output the message
    "That username or email address is already taken." ?
    In that case, a very simple modification of your code would be:

    PHP:
    if (!mysql_query($sql,$con)) {
        die(
    'That username or email address is already taken');
    }
    Now, the die function will quit your script. You may not want that to happen, so instead you would save the error message to a variable and use it later, so like:

    PHP:
    if (!mysql_query($sql,$con)) {
        
    $error_msg 'That username or email address is already taken';
    }
    and test that variable later on to make it some what more graceful.

    The only drawback here is that the SQL error might not necessarily be a duplication error, so you may be better off:
    A) A more generic error message (There was an error with your query)
    B) Handling the Error Number which is returned from PHP, and changing the error message as such.

    Hope this helps, -Maw
     
  3. designguy79 macrumors 6502

    Joined:
    Sep 24, 2009
    Location:
    Michigan
    #3
    Why not run a query that first looks for that username, and if it is indeed taken, return the correct error message? There are plenty of other reasons your other query may fail, so assuming it is because of a duplicate entry is not good practice.

    Something like... SELECT username FROM table WHERE username="incoming_user_name_to_test"

    Sorry, I am too lazy to write better PHP or SQL. :D
     
  4. CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #4
    I agree with DesignGuy. I would check to see if the record already exists before running the insert query.
     
  5. Cerebrus' Maw macrumors 6502

    Joined:
    Mar 9, 2008
    Location:
    Brisbane, Australia
    #5
    The only thing I would point out about using the select statement is that you then have to run two SQL queries, the Select and the Insert. Which wont really matter on smaller tables but nonetheless...
     
  6. CANEHDN macrumors 6502a

    CANEHDN

    Joined:
    Dec 12, 2005
    Location:
    Eagle Mountain, UT
    #6
    As long as you're indexing the fields you'd be okay. The select would find out if it's a dup or not. If the insert fails then there was an error with the sql. Since the connection errors would be found during the db connection. I can do this at work on millions of records and it's extremely fast.
     

Share This Page