【问题标题】:How to use session in model of codeigniter如何在 codeigniter 模型中使用 session
【发布时间】:2016-06-24 22:12:31
【问题描述】:

我创建了一个密码更改代码,在邮递员中我应该使用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;
    }
}

【问题讨论】:

  • 收到任何错误信息?
  • 即使我给出了有效的输入,我也会失败并且错误地认为是错误

标签: php mysql session change-password


【解决方案1】:

试试这个代码: 控制器

  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;
}

【讨论】:

  • 它在模型公共函数 checkPasswordfor($arr) 中显示错误,即使我更改为 checkPasswordfor($where, $data)
  • ( ! ) 致命错误:无法重新声明 User_model::checkPassword() in /var/www/html/sampleproject/application/models/User_model.php 第 94 行调用堆栈 # 时间记忆函数位置 1 0.0000 235112 {main}( ) ../index.php:0 2 0.0001 238432 require_once( '/var/www/html/sampleproject/system/core/CodeIgniter.php' ) ../index.php:292 3 0.0314 791888 call_user_func_array ( ) ../CodeIgniter.php:514 4 0.0314 792320 Welcome->change( ) ../CodeIgniter.php:514 5 0.0315 792920 CI_Loader->model( ) ../Welcome.php:167
  • 遇到 PHP 错误严重性:编译错误消息:无法重新声明 User_model::checkPassword() 文件名:models/User_model.php 行号:94 回溯:
  • 在控制器中注释掉这一行`$this->load->model('User_model');`
  • @vak - 希望您对给出的答案感到满意,如果您对答案感到满意,请不要忘记投票。
猜你喜欢
  • 1970-01-01
  • 2018-03-11
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2012-09-12
  • 1970-01-01
相关资源
最近更新 更多