【发布时间】:2014-05-23 14:00:51
【问题描述】:
我是 Codeigniter 的新手。任何人都知道我在哪里做错了,我的验证不起作用。我想显示一条消息,通知用户已经在数据库中,而不是出现重复的错误。谢谢您的意见。
这是我的控制器
> function User_Validation()
> {
> $this->load->helper('form');
> $this->load->library('form_validation');
>
> $session_data = $this->session->userdata('logged_in');
> $data['Admin_Name'] = $session_data['Admin_Name'];
> $data['results'] = $this->ValidUser->get_record();
>
> $my_action = $this->input->post('submit');
> if($my_action == "create")
> {
> $this->form_validation->set_rules('Ins_ID', 'Ins_ID', 'trim|required|xss_clean|callback_check_Ins_ID');
> $this->form_validation->set_rules('Password', 'Password', 'required');
> }
> if($my_action == "update")
> {
> $this->form_validation->set_rules('Ins_ID', 'Ins_ID', 'trim|required|callback_email_check');
> $this->form_validation->set_rules('Password', 'Password', 'required');
> }
> else
> {
> $this->form_validation->set_rules('Ins_ID', 'Ins_ID', 'required');
> }
>
> if ($this->form_validation->run() === FALSE)
> {
> $session_data = $this->session->userdata('logged_in');
> $data['Admin_Name'] = $session_data['Admin_Name'];
>
> $data['results'] = $this->ValidUser->get_record();
>
> $this->load->view('templates/header');
> $this->load->view('Valid_User',$data);
> $this->load->view('templates/footer');
> }
> else
> {
> $my_action = $this->input->post('submit');
> if ($my_action == 'create')
> {
> $this->ValidUser->insert_record();
> }
> if ($my_action == 'update')
> {
> $this->ValidUser->update_record();
> }
> if ($my_action == 'delete')
> {
> $this->ValidUser->delete_record();
> }
>
> $data['results'] = $this->ValidUser->get_record();
>
> $this->load->view('templates/header');
> $this->load->view('Valid_User',$data);
> $this->load->view('templates/footer');
>
> }
> }
>
> public function check_Ins_ID($Ins_ID)
> {
> //This function checks the availability of the Ins_IDin Database.
> $query = $this->db->get_where('login', array('Ins_ID' => $Ins_ID), 1);
>
> if ($query->num_rows()== 1)
> {
> return true;
> }
> else
> {
> $this->form_validation->set_message('check_ins_id', 'The User already exist in the records.Thank You');
> return false;
> }
> }
这是我的模特
> function check_ins_id($Ins_ID=null)
> {
> $this -> db -> select('Ins_ID');
> $this -> db -> from('login');
> $this -> db -> where('Ins_ID',$Ins_ID);
> $this -> db -> limit(1);
> $query = $this -> db -> get();
>
> if($query -> num_rows() == 1)
> {
> return $query->result();// doesn't return any row means database doesn't have this Ins_ID
> }
> else
> {
> return false;//if check_row return any row; that means you have already this matric no in the database.
> }
> }
对于模型中的插入功能,我需要将数据插入两个表中。这是代码
> public function insert_record()
> {
> $this->load->helper('url');
>
> $data1 = array(
>
> 'Ins_ID' => $this->input->post('Ins_ID'),
> 'Password' => $this->input->post('Password')
> );
>
> $data2 = array(
>
> 'Ins_ID' => $this->input->post('Ins_ID'),
> 'Password' => $this->input->post('Password')
> );
>
> $this->db->insert('login', $data1);
> $this->db->insert('instructor', $data2);
> }
它出现重复错误,我想用验证消息替换错误以通知用户已经存在。
错误号:1062 键 'PRIMARY' INSERT 的重复条目 '22333' INTO
instructor(Ins_ID,Password) 价值观 ('22333', '321') 文件名: C:\xampp\htdocs\CLO_Measurement_System\system\database\DB_driver.php 行号:330
【问题讨论】:
-
你有什么错误吗?您的查询看起来不错..
-
只是出于好奇,您为什么不对您的 ID 使用自动增量字段?
-
没有错误但验证不起作用。我应该显示一条消息,通知用户已经存在,但它出现重复错误。 Rick Calder:Ins_ID 不是主键
标签: php codeigniter