重复输入的警报消息

时间:2013-11-09 09:28:05

标签: php codeigniter

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

重复输入'moses@gmail.com'以获取密钥'email_address'所以我希望在警报中显示该信息请帮助我解决问题。

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;
    }

2 个答案:

答案 0 :(得分: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;
        }

}

答案 1 :(得分:0)

您可以使用flashdata课程中的session

你可以设置:

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;

Documentation