www.webdeveloper.com
Results 1 to 4 of 4

Thread: PHP-MYSQL How to check if record already exists when UPDATING?

  1. #1
    Join Date
    Sep 2013
    Posts
    3

    PHP-MYSQL How to check if record already exists when UPDATING?

    PHP Code:
    if(mysql_num_rows($result)>0)
            {
                echo 
    'User already exists';                        
            }                
            else
            {
                if(isset(
    $_POST['editid']))
                {
                    
    $id=$_POST['editid'];
                    
                    
    /* check user already exists or not */
                    
    $temp=mysql_query("SELECT 1 FROM admin_login WHERE email = '$email' AND id != '$id'");                
                    
                    
    $query=mysql_query("update admin_login set username='$uname',password='$pass',email='$email',status='$status' where id='$id';");
                    
    /*else{}*/
                
    }
                else
                {            
                    
    $query=mysql_query("insert into admin_login(username,password,email,status)values('$uname', md5('$pass'), '$email', '$status');");            
                    echo 
    "New User Created Successfully";
                }
            } 
    Last edited by NogDog; 09-10-2013 at 10:16 AM. Reason: added [php] tags

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,392
    Not sure what the actual question is?

    Possibly what you're looking for is using an "INSERT ... ON DUPLICATE KEY UPDATE ..." SQL query? (See http://dev.mysql.com/doc/refman/5.0/...duplicate.html.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2013
    Posts
    3
    Quote Originally Posted by NogDog View Post
    Not sure what the actual question is?

    Possibly what you're looking for is using an "INSERT ... ON DUPLICATE KEY UPDATE ..." SQL query? (See http://dev.mysql.com/doc/refman/5.0/...duplicate.html.)
    I set validation on duplicate value i.e email on mysql database and when i try to update user's details it will not update details on that perticular user rather than it will display "user already exists" so whats the sol. for that?? i want some generic code...

  4. #4
    Join Date
    Apr 2012
    Posts
    38
    It's really not clear what you are trying to find out.
    Things I can say:

    How to check if record already exists when UPDATING? -> you can't update something that doesn't exist.
    If the issue is that you don't want record 1 and 2 to have the same value in a certain column you could set the field in MySQL to unique value.

    In your PHP code you seem to check twice if a user already exists:

    Code:
    if(mysql_num_rows($result)>0)
            {
                echo 'User already exists';
    ...
    Code:
    /* check user already exists or not */
                    $temp=mysql_query("SELECT 1 FROM admin_login WHERE email = '$email' AND id != '$id'");

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles