几天来,我一直在尝试学习Codeigniter,在制作小型应用程序时,我已经到了必须更新DB的地步。
我已经使用验证插入了数据,但当涉及到更新时,它看起来总是"FALSE",因为这些记录已经在我正在编辑的数据库中了。结果,它不需要它。
在这里寻求一些帮助来克服这个问题。
验证(控制器):
$this->form_validation->set_rules('v_member_email', 'Email Address', 'trim|required|valid_email|callback_check_if_email_exists');
public function check_if_email_exists($requested_email) {
$email_available = $this->update_model->check_if_email_exists($requested_email);
if ($email_available) {
return TRUE;
} else {
return FALSE;
}}
它总是返回"验证错误",因为此电子邮件已在使用中。
型号:
function check_if_email_exists($email) {
$this->db->where('v_member_email', $email);
$result = $this->db->get('vbc_registered_members');
if ($result->num_rows() > 0){
return FALSE; //Email Taken
} else {
return TRUE; // Available
}}
是的,因为电子邮件已经存在。
你所要做的就是,在更新时将is传递给回调,
callback_check_if_email_exists['.$id.']
Id是数据库Id。
控制器内
public function check_if_email_exists($requested_email, $id) {
$email_available = $this->update_model->check_if_email_exists($requested_email, $id);
if ($email_available) {
return TRUE;
} else {
return FALSE;
}
}
型号
if ($id) {
$this->db->where('id !=', $id);
}
$this->db->where('email', $str);
$res = $this->db->get('users');
if ($res->num_rows()) {
return false;
} else {
return true;
}
}
我们在这里所做的是,如果你将id传递给回调,那么
检查是否存在除此id、之外的电子邮件
如果id未通过,则只检查电子邮件,而不考虑id
在控制器中,如果电子邮件存在,则返回true。如果不存在则返回false,但在模型中,如果存在则返回false,如果不存在,则返回true。
$this->form_validation->set_rules('v_member_email', 'Email Address', 'trim|required|valid_email|callback_check_if_email_exists');
public function check_if_email_exists($requested_email) {
$email_available = $this->update_model->check_if_email_exists($requested_email);
// here you check if the return from the model is true or false if true the email exists otherwise the email not exists
if ($email_available) {
return TRUE; // here true mean the email is exists and not Available
} else {
return FALSE; // here it mean the email not exists and Available
}}
如果电子邮件存在,那么这就是你应该在模型中返回true的问题。
function check_if_email_exists($email) {
$this->db->where('v_member_email', $email);
$result = $this->db->get('vbc_registered_members');
if ($result->num_rows() > 0){
return true; // here true mean the email is exists and not Available
} else {
return false; // here it mean the email not exists and Available
}
}