Home » Php » How to update form of particular id in codeigniter?

How to update form of particular id in codeigniter?

Posted by: admin October 26, 2017 Leave a comment

Questions:

I am new to codeigniter. I am stuck at update my form data. i am using one source code from internet but its not working for me. I am done with fetching data from multiple tables on one form.Now i want to update that form.I am sharing my code step by step so you will understand it.

Controller for fetching data in form:

public function __construct(){

        parent::__construct();
            $this->load->helper('url');
            $this->load->model('user_model');
            $this->load->library('session');

}


function login_user(){
  $user_login=array(

  'USER_NAME'=>$this->input->post('user_email'),
  'PASSWORD'=>($this->input->post('user_password')),
  'PIN'=>($this->input->post('user_pin'))
    );

    $data=$this->user_model->login_user($user_login['USER_NAME'],$user_login['PASSWORD'],$user_login['PIN']);

      if($data)
      {    
        $this->session->set_userdata('user_id',$data['USER_ID']);        
        $this->session->set_userdata('user_name',$data['USER_NAME']);
        $this->session->set_userdata('employee_id',$data['EMPLOYEE_ID']);
        $this->session->set_userdata('nationality',$data['NATIONALITY']);
        $this->session->set_userdata('EMPLOYEE_NAME',$data['EMPLOYEE_NAME']);
        $this->session->set_userdata('EMPLOYEE_NUMBER',$data['EMPLOYEE_NUMBER']);        
        $this->session->set_userdata('DATE_OF_BIRTH',$data['DATE_OF_BIRTH']);
        $this->session->set_userdata('JOINING_DATE',$data['JOINING_DATE']);
        $this->session->set_userdata('CURRENT_LOCATION',$data['CURRENT_LOCATION']);
        $this->session->set_userdata('SEX',$data['SEX']);
        $this->session->set_userdata('ADDRESS',$data['ADDRESS']);
        $this->session->set_userdata('COMPANY_EMPLOYEE_NUMBER',$data['COMPANY_EMPLOYEE_NUMBER']);
        $this->session->set_userdata('PHONE',$data['PHONE']);
        $this->session->set_userdata('MOBILE',$data['MOBILE']);
        $this->session->set_userdata('EMAIL_ADDRESS',$data['EMAIL_ADDRESS']);
        $this->session->set_userdata('ALTERNATE_EMAIL',$data['ALTERNATE_EMAIL']);
        $this->session->set_userdata('WORKING_HOURS',$data['WORKING_HOURS']);
        $this->session->set_userdata('NOK',$data['NOK']);
        $this->session->set_userdata('NOK_CONTACT_NUMBER',$data['NOK_CONTACT_NUMBER']);
        $this->session->set_userdata('EMPLOYMENT_STATUS_ID',$data['EMPLOYMENT_STATUS_ID']);
        $this->session->set_userdata('DESIGNATION_NAME',$data['DESIGNATION_NAME']);
        $this->session->set_userdata('CURRENT_ORG_ID',$data['CURRENT_ORG_ID']);
        $this->session->set_userdata('COS_NUMBER',$data['COS_NUMBER']);
        $this->session->set_userdata('VISA_START_DATE',$data['VISA_START_DATE']);
        $this->session->set_userdata('VISA_END_DATE',$data['VISA_END_DATE']);
        $this->session->set_userdata('DESCRIPTION',$data['DESCRIPTION']);
        $this->session->set_userdata('SKILLS',$data['SKILLS']);
        $this->session->set_userdata('YR_OF_EXPERIENCE',$data['YR_OF_EXPERIENCE']);



        $this->load->view('layouts/index.php');

      }
      else{
        $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
        $this->load->view('layouts/login.php');

      }


}

model for fetching data:

class User_model extends CI_model{


public function login_user($email,$pass,$pin){

   $this->db->select('*');
   $this->db->from('app_users');
   $this->db->where('USER_NAME',$email);
   $this->db->where('PASSWORD',$pass);
   $this->db->where('PIN',$pin);
   $this->db->join('employee_details', 'app_users.EMPLOYEE_ID = employee_details.EMPLOYEE_ID', 'LEFT');
   $this->db->join('employee_master', 'app_users.EMPLOYEE_ID = employee_master.EMPLOYEE_ID', 'LEFT');
   $this->db->join('designation_master', 'employee_details.DESIGNATION_ID = designation_master.DESIGNATION_ID', 'LEFT');
   $this->db->join('employee_visa_details', 'employee_details.EMPLOYEE_ID = employee_visa_details.EMPLOYEE_ID', 'LEFT');
   $this->db->join('visa_status_master', 'employee_visa_details.VISA_STATUS_ID = visa_status_master.VISA_STATUS_ID', 'LEFT');
   $this->db->join('employment_status_master', 'employee_details.EMPLOYMENT_STATUS_ID = employment_status_master.EMPLOYMENT_STATUS_ID', 'LEFT');             
   $this->db->join('employee_skills', 'employee_details.EMPLOYEE_ID = employee_skills.EMPLOYEE_SKILLS_ID', 'LEFT'); 
  if($query=$this->db->get())
  {
      return $query->row_array();
  }
  else{
    return false;
  }
}
}

Above files are working fine. i can see data of particular user in form view.

Now i want to update that data so i write below code:

Controller:

class Personal_details extends CI_Controller {
  public function __construct(){

          parent::__construct();
                $this->load->helper('url');
                $this->load->model('user_model');
                $this->load->model('personal_details_model');
                $this->load->library('session');

  }

  public function update_personal_details(){
      $products = new personal_details_model;
      $products->update_personal_details($id);
      redirect('/responsibilities/personal_details/');

  }

}

model for the same:

class Personal_details_model extends CI_model{

 public function update_personal_details($id) 
    {
        $data=array(
            'EMPLOYEE_NAME' => $this->input->post('emp_name'),
            'DATE_OF_BIRTH'=> $this->input->post('dob'),
            'JOINING_DATE' => $this->input->post('joining_date'),

        );
        if($id==0){
            return $this->db->insert('employee_master',$data);
        }else{
            $this->db->where('EMPLOYEE_ID',$id);
            return $this->db->update('employee_master',$data);
        }        
    }
}

I dont understand how to write code to update above all tables.

My form view is like this:

 <form action="<?php echo site_url('personal_details/update_personal_details');?>" method="post" class="form-horizontal" >
<button type="submit" class="btn btn-info">Edit</button>
<button type="submit" class="btn btn-danger">Cancel</button>

I am getting this error on click edit button:

error on update

Answers: