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

Check For Duplicate on Edit Function

Currently my DB table

    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->session->set_flashdata('errormessage', 'Duplicate');

Update Model Code

    public function updateUser($userName,$userEmail,$userAddress,$userPhone,$id);
    $UserArray = array(
    'userName' => $userName,
    'userEmail' => $userEmail,
    'userAddress' => $userAddress,
    'userPhone' => $userPhone,

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:

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.


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! ***/

Theme © iAndrew 2016 - Forum software by © MyBB