How to use session in model of codeigniter

故事扮演 提交于 2019-12-12 04:26:12

问题


I created a code for password change, in postman I should give old and new passwords with userid, when I create change password, but can't change password, it remains same in database and I think the problem is with session. Can someone see the error in my code?

Controller

  public function changepasswordcheckagain() {

    $arr = array();
    $arr['old_password'] = $this->input->post('old_password');
    $arr['new_password'] = $this->input->post('new_password');
    $this->load->model('User_model');
    $result = $this->User_model->checkPasswordfor($arr);
    echo json_encode($result);
    if ($result) {

        echo json_encode('success');exit; 
    } else {
              echo json_encode($result);
        echo json_encode('fail');exit; 

    }
}

Model

  public function checkPasswordfor($arr)
{
    $user_id = $this->session->userdata('user_id');
    $this->db->select('*');
    $this->db->from('userdetails');
    $this->db->where('user_id',$user_id);
    //$this->db->where('user_password',$arr['old_password']);
    $query=$this->db->get();
    $result=$query->result();
    if($result){
        $layout['user_password'] = $arr['old_password'];
        $layout_data['user_password'] = $arr['new_password'];

        $this->db->where('user_id',$user_id);
        $this->db->update('userdetails',$layout_data, $layout);
        return true;
    }else{
        return false;
    }
}

回答1:


try this code: controller

  public function changepasswordcheckagain() {

        $user_id = $this->session->userdata('user_id');

        $arr = array();
        $old_password = $this->input->post('old_password');
        $new_password = $this->input->post('new_password');

        $this->load->model('User_model');
        $where = ['user_id' => $user_id];

        //$data['password'] ---> column name 
        //$data['password' => $new_password] ---> new value of column
        $data = [
            'password' => $new_password
        ];

        $result = $this->User_model->checkPasswordfor($where, $data);

        echo json_encode($result);
        if ($result == TRUE) {

            echo json_encode('success');exit; 
        } else {
                  echo json_encode($result);
            echo json_encode('fail');exit; 

        }
  }

model

   public function checkPasswordfor($arr)
   {
        $this->db->where($where);
        if($this->db->update('table name',$data))
        {
            return TRUE;
        }
        else
            return FALSE;
}


来源:https://stackoverflow.com/questions/35910185/how-to-use-session-in-model-of-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!