Codeigniter 编辑(检查重复项 + 如果未找到重复项则允许更新)

Codeigniter Edit (Check Duplicate + Allow to Update if duplicate not found)

检查编辑功能是否重复

目前我的数据库table

USERDB
userId userName userPassword userEmail userAddress userPhone

我想要的

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

遇到的问题:

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");
  }

更新模型代码

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');        
}

好吧,由于用户可以编辑他们的信息,我假设他们已经登录。在这种情况下,您需要从会话或任何地方获取他们当前的用户名并执行类似的操作:

伪代码:

if ($isNotDuplicate || $postedUsername == $sessOrDbUsername) {

    // update

}

这样,当用户只想编辑他们的详细信息并且表单提交时他们的用户名与他们拥有的用户名相同,也就是重复的,我们可以简单地进行另一次检查以确定他们当前的用户名是否与一个带有 post 请求。如果是,我们继续更新。

对于这个问题,你必须发送用户ID,在哪个函数中你可以检查用户名的重复。

$this->userdb->UsernameDuplicatechecking($userName,$id);

将用户 ID 发送到函数 UsernameDuplicatechecking 后。添加一个 where 条件 userID 不等于当前 ID。

$this->db->where("userId <> $id");

希望这能解决您的问题。