Codeigniter 检查编辑功能是否重复
Codeigniter Check For Duplicate on Edit Function
检查编辑功能是否重复
目前我的数据库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 ($query == true)。检查 if ($validation == true) 并且您的 else 条件是错误的,就好像用户名重复一样,它将进入 if 条件。
考虑到您的用户名输入名为 userName
,一个解决方案是您可以添加条件 is_unique
验证,这样如果用户不更改 his/her 用户名,那么就不要更改t 将其验证为唯一。
这可以通过 id 从数据库中提取用户的 userName
并将其与发布的 userName
值进行比较来完成:
$id = $this->input->post('id');
$old_username = $this->db->get_where('USERDB', array('userId' => $id))->row_array();
$original_value = $old_username['userName'];
if($this->input->post('userName') != $original_value) {
$is_unique = '|is_unique[USERDB.userName]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('userName', 'User Name', 'required|trim|xss_clean'.$is_unique);
能否请您分享函数 UsernameDuplicatechecking($userName) 的代码,或者您可以尝试以下解决方案,如果有帮助请告诉我。将以下功能添加到您的 userdb 模型
function checkDuplicateUsername($userName)
{
$this->db->where('userName',$userName);
$query = $this->db->get('USERDB');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
现在更新你的控制器功能如下:
$checkvalidation = $this->userdb->checkDuplicateUsername($userName);
if($checkvalidation == true){
$this->userdb->updateUser($userName,$userEmail,$userAddress,$userPhone,$id);
}else{
$this->session->set_flashdata('errormessage', 'Your message here');
redirect("User/EditUserAccount/$id");
}
检查编辑功能是否重复
目前我的数据库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 ($query == true)。检查 if ($validation == true) 并且您的 else 条件是错误的,就好像用户名重复一样,它将进入 if 条件。
考虑到您的用户名输入名为 userName
,一个解决方案是您可以添加条件 is_unique
验证,这样如果用户不更改 his/her 用户名,那么就不要更改t 将其验证为唯一。
这可以通过 id 从数据库中提取用户的 userName
并将其与发布的 userName
值进行比较来完成:
$id = $this->input->post('id');
$old_username = $this->db->get_where('USERDB', array('userId' => $id))->row_array();
$original_value = $old_username['userName'];
if($this->input->post('userName') != $original_value) {
$is_unique = '|is_unique[USERDB.userName]'
} else {
$is_unique = ''
}
$this->form_validation->set_rules('userName', 'User Name', 'required|trim|xss_clean'.$is_unique);
能否请您分享函数 UsernameDuplicatechecking($userName) 的代码,或者您可以尝试以下解决方案,如果有帮助请告诉我。将以下功能添加到您的 userdb 模型
function checkDuplicateUsername($userName)
{
$this->db->where('userName',$userName);
$query = $this->db->get('USERDB');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
现在更新你的控制器功能如下:
$checkvalidation = $this->userdb->checkDuplicateUsername($userName);
if($checkvalidation == true){
$this->userdb->updateUser($userName,$userEmail,$userAddress,$userPhone,$id);
}else{
$this->session->set_flashdata('errormessage', 'Your message here');
redirect("User/EditUserAccount/$id");
}