如何在codeigniter模型中使用session

时间:2016-03-10 07:29:37

标签: php mysql session change-password

我创建了一个密码更改代码,邮递员我应该用userid给新旧密码,当我创建更改密码但不能更改密码时,它在数据库中保持相同,我认为问题在于会话。有人可以在我的代码中看到错误吗?

控制器

  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; 

    }
}

模型

  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 个答案:

答案 0 :(得分:0)

试试这段代码: 的控制器

  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; 

        }
  }

<强>模型

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