如何检查回调验证codeigniter重复数据库条目?

时间:2014-07-31 15:33:24

标签: php validation codeigniter

这是我的回调函数,我想检查数据库的重复值,我已经尝试了很多,但我无法进行验证工作。我是Codeigniter的新手,所以任何帮助都会受到赞赏!

    public function alias_exist_check()
    {
        $scol_code = $this->input->post('school_code');
        $user_id=$this->input->post('user_id');
        $query=$this->db->get_where('user_application',array('school_code'=>$scol_code,                                                                                 'user_id'=>$user_id));
        $row= $query->row_array();

        if(!$row['user_id']==$user_id && !$row['school_code']==$scol_code)
        {
            return TRUE;

        } else {

            $this->form_validation->set_message('alias_exist_check', 'Already exists.');
            return FALSE;                    
        }
    } 

UPDATE1 :: 我试过这个,但是如果我写错了,那就不帮我了。

$this->form_validation->set_rules('school_code', 'School Name','required','callback_alias_exist_check', 'trim|xss_clean'); $where = array(
  'school_code' => $this->input->post('school_code'),
  'user_id' => $this->input->post('post'));

if( ! $this->lawschool_model->alias_exist_check($where))
{
      $this->form_validation->set_message('alias_exist_check', 'Already exists.');
                            }
     if ($this->form_validation->run() == FALSE)
    {
      $data['row']=  $this->lawschool_model->Getuser($data1);
      $data['row1']=  $this->lawschool_model->GetData1();
      $this->ag_auth->view('Home',$data);
    }
    else
    {
       $insert = $this->db->insert('user_application',$data);       

    if($insert==TRUE)
    {
        /*$idNum = $this->input->post('school_code');
        $data1 = $this->lawschool_model->upddata_school();*/

        $data['row'] =  $this->lawschool_model->Getuser($data1);
        $data['row1'] =  $this->lawschool_model->GetData1();
        $this->ag_auth->view('Home',$data);
     }
}

UPDATE2 ::最终它的工作正常,这是我的工作代码

   $this->form_validation->set_rules('school_code', 'School Name','required','callback_alias_exist_check1', 'trim|xss_clean'); 

   function alias_exist_check1($scol_code,$user_id)
   {
        $sql = "SELECT * FROM user_application WHERE school_code = ? AND user_id = ?";
        $val = $this->db->query($sql,array($scol_code ,$user_id ));

        if ($val->num_rows)
        {               
          $this->form_validation->set_message('alias_exist_check', 'Already exists.');
          return TRUE;
        }
        else
        {
          return FALSE;
        }
    }

2 个答案:

答案 0 :(得分:1)

模型

public function alias_exist($where)
{
    return $this->db->where($where)->count_all_results('user_application') > 0;
}

控制器

public function alias_exist_check()
{
    $where = array(
        'school_code' => $this->input->post('school_code'),
        'user_id'     => $this->input->post('user_id')
    );
    return ! $this->name_model->alias_exist($where);
}

答案 1 :(得分:0)

第一个函数无效,因为您试图从回调本身访问帖子数据。这似乎不适用于回调。这是因为一旦您运行表单验证器运行方法,codeigniter将从请求中删除所有发布数据。它仅在表单处理完成时重新填充后期数据。传递回调函数所需的任何额外参数,以便像这样工作

callback_foo[bar]