如何在codeigniter模型中使用会话 [英] How to use session in model of codeigniter
本文介绍了如何在codeigniter模型中使用会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个密码更改代码,在邮递员中我应该使用 userid
提供旧密码和新密码,当我创建更改密码但无法更改密码时,它在数据库中保持不变,我认为问题出在会话上.有人能看到我代码中的错误吗?
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?
控制器
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;
}
}
推荐答案
试试这个代码:控制器
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;
}
这篇关于如何在codeigniter模型中使用会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文