php - 检查 CodeIgniter 中的重复数据尝试创建回调函数

标签 php mysql codeigniter duplicates

我有一张注册表。在这里我可以通过我的自定义是唯一回调函数检查重复的电子邮件(不要尝试使用 is_unique)。但它不返回任何东西。这是我的代码。

Controller -

public function add_member () {

    $this->load->library('form_validation');

    $post_email = $this->input->post('email_id');
    $this->form_validation->set_rules('email_id', 'Email ID/ Username', 'required|trim|xss_clean|valid_email|callback_check_duplicate_email[' . $post_email . ']');
    $this->form_validation->set_rules('password', 'Your password', 'required|min_length[5]|max_length[12]|matches[confirm_password');
    $this->form_validation->set_rules('confirm_password', 'Password Confirmation', 'required');

    $this->form_validation->set_message('check_duplicate_email', 'This email is already exist. Please write a new email.');

    if ($this->form_validation->run() == FALSE) {
        // validation failed then do that

        $this->load->view('member/reg_form');

    } else {

        $data['email_id'] = $post_email;
        $data['password'] = MD5($this->input->post('password'));

        $insert = $this->Model_member->insert_data_to_db($data);

        if ($insert) {
            $success = "Wao ! You are successfully added to our community.";
            $this->session->set_flashdata('message_success', $success);
            $this->load->view('member/success_page');
        } else {
            $error = "Hey this email is already exists in our community.";
            $this->session->set_flashdata('message_error', $error);
            $this->load->view('member/reg_form');
        }
    }

}

// My callback function
public function check_duplicate_email($post_email) {

    return $this->Model_member->checkDuplicateEmail($post_email);

}

模型 -

 //for checking email existance
 public function checkDuplicateEmail($post_email) {

    $this->db->where('email_id', $email_id);

    $query = $this->db->get('my_registration_table');

    $count_row = $query->num_rows();

    if ($count_row > 0) {
        return TRUE;
    } else {
        return FALSE;
    }
}

// insert data to DB
public function insert_data_to_db($data) {
    return $this->db->insert('my_registration_table', $data);
}

当我尝试使用已存在的电子邮件提交表单时。此功能不会阻止我,也不会显示验证错误消息。有人会在这里看看有什么问题吗?等待您的帮助。

最佳答案

您的问题出在模型函数中。看下面的模型函数。

public function checkDuplicateEmail($post_email) {

    $this->db->where('email_id', $email_id);

    $query = $this->db->get('my_registration_table');

    $count_row = $query->num_rows();

    if ($count_row > 0) {
      //if count row return any row; that means you have already this email address in the database. so you must set false in this sense.
        return FALSE; // here I change TRUE to false.
     } else {
      // doesn't return any row means database doesn't have this email
        return TRUE; // And here false to TRUE
     }
}

试试这个。希望有效。

关于php - 检查 CodeIgniter 中的重复数据尝试创建回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18493043/

相关文章:

php - Symfony2 Doctrine : syntax error, 意外 'function' ,期待 'identifier'

php - 将 KML 文件加载到 mysql/xpath 和 x quires

PHP使用来自 Composer 的自动加载器添加自定义命名空间

php - cakephp 3.8 中的多次插入

重温PHP显示问题

php - MySQL 数据库 I18N,一种 JSON 方法?

html - Twig 和 CodeIgniter 表单渲染问题。表单显示为字符串而不是 HTML 表单

Mysql Sql group by 之前的顺序

javascript - 如何在codeigniter中调用提交函数

php - mysql,获取最后一个类别中的最后一个文章