phpcodeignitermodel-view-controllerget-request

Undefined variable $id in CodeIgniter controller


I'm trying to update a record in a CI project. I can deal with the form validation later. Right now I'm trying to address the inability to update the record in the database.

I'm using a template for all pages and placing all content. Any help would be appreciated.

Model for updating record

 public function up_ct_acct($id, $sus) 
 {  
 $this->db->where('user_id',$id);  
  $this->db->update('users',$sus); 
 }  

Controller for form view and action

//for page view
public function update_profile($id, $datum)
{ 
   //the username is on the url.
   $id = $this->uri->segment(3);    
   $datum['user_profile'] = $this->user_model->get_da_profile($id);
   //using a template for all pages
   $content['main_content'] = $this->load->view('users/update_profile',$datum); 
   $this->load->view('main',$content); 
} 
//action to update 
public function up_sup_acct() 
{ 
      $first_name = $this->input->post('first_name'); 
      $last_name = $this->input->post('last_name');  
      $phone = $this->input->post('phone'); 
      $mobile = $this->input->post('mobile'); 
      $city = $this->input->post('city'); 
      $country = $this->input->post('country'); 
      $description = $this->input->post('description'); 
      $date_edited = $this->input->post('today');     

      $sus = array( 
         'first_name' => $first_name, 
         'last_name' => $last_name, 
         'phone' => $phone, 
         'mobile' => $mobile, 
         'city' => $city,
         'country' => $country, 
         'description' => $description, 
         'date_edited' => $date_edited
         );

      $this->user_model->up_ct_acct($id,$sus);      
} 

Solution

  • Some thing more simple is to put post data in model and id like so $this->db->where('user_id',$this->uri->segment(3));

    <?php
    
    class Users extends CI_Controller {
    
        public function update_profile($id, $datum) { 
            $id = $this->uri->segment(3);    
            $datum['user_profile'] = $this->user_model->get_da_profile($id);
            $content['main_content'] = $this->load->view('users/update_profile',$datum); 
            $this->load->view('main',$content); 
        } 
    
        public function up_sup_acct() { 
            $this->load->model('user_model');
            $this->user_model->up_ct_acct();      
        } 
    
    }
    
    ?>
    

    Model

    <?php
    
    class User_model extends CI_Model {
    
        public function up_ct_acct() {
            $data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'phone' => $this->input->post('phone'),
                'mobile' => $this->input->post('mobile'),
                'city' => $this->input->post('city'),
                'country' => $this->input->post('country'),
                'description' => $this->input->post('description'),
                'date_edited' => $this->input->post('today') 
            );
    
            $this->db->where('user_id',$this->uri->segment(3));  
            $this->db->update('users',$data); 
        }
    }