更新数据库记录时回调函数的相反结果
Callback function's opposite results while updating database record
这几天我一直在尝试学习 Codeigniter,在制作小型应用程序时,我不得不更新数据库。
我已使用验证插入数据,但在更新时,它看起来总是 "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;
}}
它总是 returns "Validation Error" 因为此电子邮件已被使用。
型号:
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
在您的控制器中,如果电子邮件存在,您 return 为真。如果不存在则为 false,但在您的模型中 return 如果存在则为 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
}}
这就是你应该 return 在你的模型中解决的问题,如果电子邮件存在以使其工作。
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
}
}
这几天我一直在尝试学习 Codeigniter,在制作小型应用程序时,我不得不更新数据库。
我已使用验证插入数据,但在更新时,它看起来总是 "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;
}}
它总是 returns "Validation Error" 因为此电子邮件已被使用。
型号:
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
在您的控制器中,如果电子邮件存在,您 return 为真。如果不存在则为 false,但在您的模型中 return 如果存在则为 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
}}
这就是你应该 return 在你的模型中解决的问题,如果电子邮件存在以使其工作。
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
}
}