Skip to content
Advertisement

codeigniter google login with existing gmail account creating duplicate row

I have implemented google login in existing user table in coeigniter. User can login whether with existing email and password or from google login. Everything working fine but i need to check with existing email. Suppose that gmail already in the database i need to update the oauth_provider and oauth_uid in the existing row. Right now creating new row for google login. So my emails are duplicated.

public function checkUser($data = array()){

    $this->db->select('id');
    $this->db->from($this->tableName);
    
    $con = array(
        'oauth_provider' => $data['oauth_provider'],
        'oauth_uid' => $data['oauth_uid']
    );
    $this->db->where($con);
    $query = $this->db->get();

    //echo $this->db->last_query(); die;
    
    $check = $query->num_rows();
    if($check > 0){
        // Get prev user data
  $result = $query->row_array();
        
        
        // Update user data
        $data['edited_dt'] = date("Y-m-d H:i:s");
        $update = $this->db->update($this->tableName, $data, array('id' => $result['id']));
        
        // Get user ID
        $userID = $result['id'];
    }else{
        // Insert user data
        $data['created_dt'] = date("Y-m-d H:i:s");
        $data['edited_dt'] = date("Y-m-d H:i:s");
        $insert = $this->db->insert($this->tableName, $data);
        
        // Get user ID
        $userID = $this->db->insert_id();
    }
    
    // Return user ID
    return $userID?$userID:false;
}

Advertisement

Answer

In your code above you are not checking if the email already exists in your database. What you are doing is just finding against oauth_provider and oauth_uid if record is found you are updating that record which is fine. But when it’s the first time user login using gmail, it won’t find any record so it will create a new record, now here before creating new record you need to check if current email already exists you need to update the existing record else create new record.

your insert code

}else{
    // Insert user data
    $data['created_dt'] = date("Y-m-d H:i:s");
    $data['edited_dt'] = date("Y-m-d H:i:s");
    $insert = $this->db->insert($this->tableName, $data);
    
    // Get user ID
    $userID = $this->db->insert_id();
}

Note: Make sure you edit the column name and code according to your database design.

Corrected insert code

}else{
    
    //check if email already exist
    $this->db->select('id');
    $this->db->from($this->tableName);
    
    $con = array(
        'email' => $data['email']
    );
    $this->db->where($con);
    $query = $this->db->get();
    
    $check = $query->num_rows();
    
    if($check > 0){
        // Get prev user data
        $result = $query->row_array();
        
        // Update user data
        
        //here update whatever data you want to update for that record
        $data['edited_dt'] = date("Y-m-d H:i:s");
        $update = $this->db->update($this->tableName, $data, array('id' => $result['id']));
        
        // Get user ID
        $userID = $result['id'];

    }else{
        //Insert new record            
        
        // Insert user data
        $data['created_dt'] = date("Y-m-d H:i:s");
        $data['edited_dt'] = date("Y-m-d H:i:s");
        $insert = $this->db->insert($this->tableName, $data);
        
        // Get user ID
        $userID = $this->db->insert_id();
    }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement