Mark Anthony Naluz
Mark Anthony Naluz

Reputation: 27

How to update data using session in codeigniter

This my code in codeigniter but it doesn't update in database, I'm beginner ni codeigniter, how could I fix this error, or what is wrong in my code? THis is my Controller:

function edit() {
    $role = $this->session->userdata('role');
    $this->form_validation->set_rules('firstname', 'firstname', 'required|xss_clean');
    $this->form_validation->set_rules('lastname', 'lastname', 'required|xss_clean');
    if ($this->form_validation->run() == FALSE) {
        //set page data
        $data['title'] = 'Update Profile';
       if($role!=''){
       $data['admin'] = $this->M_user->get($this->session->userdata('user_id'));
      }else{
      $data['admin'] = $this->M_administrator->getAdmin($this->session->userdata('id_admin'));
      }
        $data['sitename'] = $this->M_website->getName();
        $data['content'] = 'admin/myaccount/edit';

        //parse template
        $this->parser->parse('admin/template', $data);
    } else {
       if($role!=''){
        if ($this->M_user->updateStatus($_POST['user_id'])) {
             //SAVE ADMIN ACTION LOG
            //save_admin_action(array('module' => Constant::AM_ACCOUNT, 'action' => Constant::AL_EDIT, 'title' => $this->form_validation['username'], 'object_id' => $id));

            //redirect page
            $this->session->set_flashdata('saved', TRUE);
            redirect('admin/myaccount');
        }
       }else{           
        if ($this->M_administrator->updateStatus($_POST['id_admin'])) {
             //SAVE ADMIN ACTION LOG
            //save_admin_action(array('module' => Constant::AM_ACCOUNT, 'action' => Constant::AL_EDIT, 'title' => $this->form_validation['username'], 'object_id' => $id));

            //redirect page
            $this->session->set_flashdata('saved', TRUE);
            redirect('admin/myaccount');
        }
      }
    }
}

This is my model administrator:

function updateStatus($post, $id){
    $data = array(
                'firstname' => $post['firstname'],
                'lastname' => $post['lastname']
        );
    $this->db->where('id_admin', $id);
    if($this->db->update('admin', $data)){
        return TRUE;
    }else{
        return FALSE;   
    }
}

user Model:

function updateStatus($post, $id){
    $data = array(
                'firstname' => $post['firstname'],
                'lastname' => $post['lastname']
        );
    $this->db->where('user_id', $id);
    if($this->db->update('user', $data)){
        return TRUE;
    }else{
        return FALSE;
    }
}

Upvotes: 0

Views: 747

Answers (2)

Trushar Narodia
Trushar Narodia

Reputation: 366

pass firstname and last name to your updatestatus model function if you are not getting that value in model so you are not able to change

print your query using $this->db->last_query(); to get query output and post your query here

Upvotes: 1

The developer
The developer

Reputation: 194

Change your where clause to $this->db->where('id_admin', $post);

Upvotes: 0

Related Questions