如何在codeigniter中更新特定id的表单?

时间:2017-10-26 06:59:14

标签: php codeigniter sql-update crud

我是codeigniter的新手。我被困在更新我的表格数据。我正在使用一个来自互联网的源代码,但它不适合我。我已经完成从一个表单上的多个表中获取数据。现在我想更新该表单。我正在逐步共享我的代码,以便您理解它。

用于以表格形式获取数据的控制器:

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

      }


}

获取数据的模型:

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

上面的文件工作正常。我可以在表单视图中看到特定用户的数据。

现在我想更新这些数据,所以我写下代码:

控制器:

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/');

  }

}

相同的模型:

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

我不明白如何编写代码来更新所有表格。

我的表单视图是这样的:

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

我在点击修改按钮上收到此错误:

error on update

1 个答案:

答案 0 :(得分:0)

首先,您必须在Personal_details控制器中获取表单数据,然后将该数据传递给Personal_details_model。其次,无需在update_personal_details()函数中为模型创建新对象,您已在构造函数中加载了personal_details_model。