Welcome Guest, Not a member yet? Register   Sign In
Codeigniter Check For Duplicate on Edit Function
#1

Check For Duplicate on Edit Function

Currently my DB table

    USERDB
    userId userName userPassword userEmail userAddress userPhone


What I want

> Allow to check on "userName" duplication" and if "userName" return
> true , then it allow to edit other textfield.

Problem Met:

> When i did not change anything on my "userName" field , and i edited
> userEmail, it will always return FALSE due to my "userName" is
> duplicate with current DB data.


    $checkvalidation = $this->userdb->UsernameDuplicatechecking($userName);
    if($checkvalidation == TRUE){
    $this->userdb->updateUser($userName,$userEmail,$userAddress,$userPhone,$id);
    }else{
      $this->session->set_flashdata('errormessage', 'Duplicate');
      redirect("User/EditUserAccount/$id");
      }

Update Model Code

    public function updateUser($userName,$userEmail,$userAddress,$userPhone,$id);
    {
    $UserArray = array(
    'userName' => $userName,
    'userEmail' => $userEmail,
    'userAddress' => $userAddress,
    'userPhone' => $userPhone,
      );
   
   
        $this->db->set($UserArray);
    $this->db->where('userId',$id);
    $this->db->update('USERDB');
    }
Reply
#2

Yes this can be a pain. You need two functions, one for registration and one for update. When updating I do two queries, one to see if the username already exists first but exclude the current user id.

I also check if the username has actually changed or not. In fact when a user is logged in I have separate pages, so the user can change their email address, or change their username, on separate forms.

Alternatively you could add an additional value to your function call, if the userId if it exists:

PHP Code:
$this->userdb->UsernameDuplicatechecking($userName$userID); 

Then in your function something like:

PHP Code:
public function UsernameDuplicatechecking($userName$userID=0)
{
 
  ….some code here

   
if ($userID 0)
 
  {
 
      $this->db->where('userID !='$userID);
 
  }

 
  …more code here


That will exclude your current user record if your user is logged in.

I hope that helps in some way. But yes what you describe can be a pain.

Paul
Reply
#3

Or you can simply use the is_unique validation rule.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB