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 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    224
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to alet user if MySQL data inserted is not unique.

    Currently, if someone enters data in a unique column that is not unique, it just looks like it was inserted, but it wasn't.

    How do I do something like this:

    PHP Code:
    if ([Data insertedis not unique){
    echo 
    '';

    I know I could do a foreach loop and check it against every value, but since MySQL already knows it's unique, how do I tell this to the user?

  • #2
    New Coder
    Join Date
    Dec 2006
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if a new row is inserted you can use mysql_insert_id() function to retrieve the id of the inserted row. If no row was inserted it will return null.

    PHP Code:
    mysql_query($sql_that_inserts_row_maybe);
    if(
    mysql_insert_id())
      echo 
    "Row inserted";
    else
      echo 
    "Row was not inserted, duplicate content"
    Also i think mysql_error() holds a message if you try to enter unique data, however displaying that to the user can be a security risk should there ever happen any other errors.

    So if you really want to check if a value exists already a simple query beforehand is probably the best way to go, but who knows i dont consider myself a sql pro
    Last edited by vegu; 01-04-2007 at 05:07 PM.
    http://demo.vegui.org - vegUI AJAX framework
    http://www.landsofkazram.com - browser based graphical MMORPG

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    224
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks! That worked perfectly


  •  

    Posting Permissions

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