0
votes

i am newbie in Codeigniter. Anyone know where I did wrong,my validation does not function.i want to display a message to inform that the user is already in the database instead of a duplicate error come out.thank you for your opinion.

this is my controller

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

this is my model

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

for the insert function in model,i need to insert the data into two table.here is the code

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

it come out with the duplicate error,i want replace the error with validation message to inform that the user already exist.

Error Number: 1062 Duplicate entry '22333' for key 'PRIMARY' INSERT INTO instructor (Ins_ID, Password) VALUES ('22333', '321') Filename: C:\xampp\htdocs\CLO_Measurement_System\system\database\DB_driver.php Line Number: 330

2
You get any error? Your query looks like well.. - Bora
Just out of curiosity why aren't you using an autoincrement field for your ID's? - Rick Calder
no error but the validation does not function. i should be display a message to inform that the user already exist, but it come out with duplicate error. Rick Calder: Ins_ID not a primary key - Carson

2 Answers

0
votes

You write returns reverse. if ($query->num_rows()== 1) mean user exists and return false with error else mean not found and 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;
    }
}
0
votes

If I am understanding you right, you could use the "is_unique" rule?

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