title: how to check callback validation codeigniter duplicate database entry?
body: <p>This is my callback function, I want to check the database for duplicate value, I have tried a lot, but I can't get validation to work. I'm new to Codeigniter so any help would be appreciated!</p>
  
  <pre><code>    public function alias_exist_check()
      {
          $scol_code = $this-&gt;input-&gt;post('school_code');
          $user_id=$this-&gt;input-&gt;post('user_id');
          $query=$this-&gt;db-&gt;get_where('user_application',array('school_code'=&gt;$scol_code,                                                                                 'user_id'=&gt;$user_id));
          $row= $query-&gt;row_array();
  
          if(!$row['user_id']==$user_id &amp;&amp; !$row['school_code']==$scol_code)
          {
              return TRUE;
  
          } else {
  
              $this-&gt;form_validation-&gt;set_message('alias_exist_check', 'Already exists.');
              return FALSE;                    
          }
      } 
  </code></pre>
  
  <p>UPDATE1 ::
  i tried this but its not working me help me if i wrote any mistakes.</p>
  
  <pre><code>$this-&gt;form_validation-&gt;set_rules('school_code', 'School Name','required','callback_alias_exist_check', 'trim|xss_clean'); $where = array(
    'school_code' =&gt; $this-&gt;input-&gt;post('school_code'),
    'user_id' =&gt; $this-&gt;input-&gt;post('post'));
  
  if( ! $this-&gt;lawschool_model-&gt;alias_exist_check($where))
  {
        $this-&gt;form_validation-&gt;set_message('alias_exist_check', 'Already exists.');
                              }
       if ($this-&gt;form_validation-&gt;run() == FALSE)
      {
        $data['row']=  $this-&gt;lawschool_model-&gt;Getuser($data1);
        $data['row1']=  $this-&gt;lawschool_model-&gt;GetData1();
        $this-&gt;ag_auth-&gt;view('Home',$data);
      }
      else
      {
         $insert = $this-&gt;db-&gt;insert('user_application',$data);       
  
      if($insert==TRUE)
      {
          /*$idNum = $this-&gt;input-&gt;post('school_code');
          $data1 = $this-&gt;lawschool_model-&gt;upddata_school();*/
  
          $data['row'] =  $this-&gt;lawschool_model-&gt;Getuser($data1);
          $data['row1'] =  $this-&gt;lawschool_model-&gt;GetData1();
          $this-&gt;ag_auth-&gt;view('Home',$data);
       }
  }
  </code></pre>
  
  <p>UPDATE2::finaly its works fine,here is my working code</p>
  
  <pre><code>   $this-&gt;form_validation-&gt;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-&gt;db-&gt;query($sql,array($scol_code ,$user_id ));
  
          if ($val-&gt;num_rows)
          {               
            $this-&gt;form_validation-&gt;set_message('alias_exist_check', 'Already exists.');
            return TRUE;
          }
          else
          {
            return FALSE;
          }
      }
  </code></pre>