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 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Checking for existing record in MYSQL

    Hi everyone

    I'm a new user of this site. I'm sorry if this question has been asked before but I had no luck finding the answers.

    I've got a mysql database with a single table. I also have a page containing a FORM with a number of fields that once submitted is stored into the database.

    My problem is, users are able store the same information more than once. How do i check if that record already exists before inserting into mysql table?

    For example, I want to check that, that particular username, nickname and email exists in the table. If it does, print out a warning. If it doesnt exist, go ahead and perform the query and insert the record.

    Any help would be much appreciated.

    Eddie.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    PHP Code:

    if(mysql_query("SELECT * FROM table WHERE title =  `" .  $user_suggested_title  "`")) {
      die(
    "There is already an entry with this title!");

    Last edited by Element; 01-08-2006 at 10:18 PM.

  • #3
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    The previous post's code will return a resource regardless of a match:
    PHP Code:
    if(mysql_num_rows(mysql_query("SELECT * FROM table WHERE title =  `" .  $user_suggested_title  "`"))>0) { 
      die(
    "There is already an entry with this title!"); 


  • #4
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply guys..

    What about if i want to check against more than one field in my table..
    For example, i would like to check the following fields exists or not.

    firstname, lastname and email

    if All the above match, then do not insert.. otherwise go ahead with the insert.


    Thanks again in advance
    Eddie.

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    PHP Code:
    if (mysql_num_rows(mysql_query('SELECT COUNT(*) FROM table WHERE firstname=\'' $firstname '\' AND lastname=\'' $lastname '\' AND email=\'' $email '\' LIMIT 1')) == 0)
    {
        
    // insert
    }
    else
    {
        
    // duplicate

    Last edited by ralph l mayo; 01-08-2006 at 11:54 PM.

  • #6
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Ralph..
    I believe that was the code i was looking for.



    Eddie

  • #7
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    You're welcome Eddie, good luck.


  •  

    Posting Permissions

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