【问题标题】:Validation in codeigniter在 codeigniter 中验证
【发布时间】: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


【解决方案1】:

你写 returns 反向。 if ($query->num_rows()== 1) 表示用户存在,return falseerror 否则表示未找到,return true

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)
    {
        $this->form_validation->set_message('check_ins_id', 'The User already exist in the records.Thank You');
        return false;
    }
    else
    {
        return true;
    }
}

【讨论】:

  • 由于我需要将数据插入两个表中,是否需要添加 $query = $this->db->get_where('instructor', array('Ins_ID' => $Ins_ID) , 1);?重复错误表明讲师表中出现重复。
  • @Carson 您要检查两个表并显示错误吗?
  • 是的,因为我需要将数据插入两个表中。
  • 如果你总是插入两个表,你不需要检查两个表。如果登录中存在,则讲师表中已经存在..事实上,我不明白你想要做什么
  • 是的。现在我想在验证时使用回调函数来显示一条消息,如果用户插入重复数据而不是重复错误。所以,用户将插入另一个数据。
【解决方案2】:

如果我的理解正确,您可以使用“is_unique”规则吗?

$this->form_validation->set_rules('Ins_ID', 'Ins_ID', 'trim|required|xss_clean|is_unique[login.Ins_ID]');

【讨论】:

    猜你喜欢
    • 2012-05-21
    • 2012-01-19
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 2019-03-12
    • 2012-06-03
    • 2016-12-14
    • 2012-02-27
    相关资源
    最近更新 更多