0

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;
    }
}
4
  • I think that you are passing only one parameter in your function i.e $this->M_user->updateStatus($_POST['user_id']) in model function updateStatus($post, $id) and you are using the second parameter i.e $id in where which will be blank Commented Jan 4, 2017 at 5:57
  • So i only declare $post but the results is the same. Commented Jan 4, 2017 at 8:36
  • are you able to access the value of $post['firstname'] in model Commented Jan 4, 2017 at 9:37
  • yes i want to access firstname and lastname in model, i want to update firstname and lastname value, Commented Jan 4, 2017 at 9:50

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

When i replace $id to $post it store 2, and it never change,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.