0

我一直在 codeigniter 中工作,我为电子邮件地址应用了唯一密钥。所以当我输入重复的电子邮件地址时,它显示错误号:1062

密钥“email_address”的重复条目“moses@gmail.com”所以我希望它显示在警报中,请帮助我解决问题。

function create_member()
    {

        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email_address' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password'))                       
        );

        $insert = $this->db->insert('membership', $new_member_insert_data);
        if ($this->db->_error_number() == 1062)
                {
                echo "Duplicate value";
                }
        return $insert;
    }
4

2 回答 2

1

尝试这个:

// Controller "membership"

function register() {

    if($this->input->post()){

        $this->form_validation->set_rules('email_address', 'Email', 'trim|required|xss_clean|is_unique[membership.email_address]');

        if ($this->form_validation->run() == TRUE){

            $this->load->model('membership_model');
            $this->membership_model->create_member();

        }

    }

}


// Model "membership_model"

function create_member() {

        $new_member_insert_data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'email_address' => $this->input->post('email_address'),         
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password'))                       
        );

        if ($this->db->insert('membership', $new_member_insert_data)) {
            return TRUE;
        }

}
于 2013-11-09T09:39:39.827 回答
0

您可以使用flashdatafromsession类。

你可以设置:

if ($this->db->_error_number() == 1062)
{
$this->session->set_flashdata('duplicate_email', 'Duplicate value');
redirect('your_previous_page');
}

并在“查看”页面中获取duplicate_email值并显示错误消息:

$err = $this->session->flashdata('duplicate_email');

echo $err;

文档

于 2013-11-09T09:37:06.103 回答