codeigniter 当我想使用代码点火器访问数据库中的某个列时,如何解决未定义索引错误?

0x6upsns  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(78)

我想验证登录用户的密码是否与数据库中的密码匹配。但是,由于某种原因,即使我尝试回显该密码,也无法提取该数据。)但是json_encode工作正常,如postman结果所示。

控制器

function authentication(){
    $stdnum = $this->input->post('student_number');
    $password = $this->input->post('password');
    $response = $this->api_model->login($stdnum);
    // echo $password;
    // echo $response['password'];
    echo json_encode($response);
    $result = array();
    $result['login'] = array();
            
    if (isset($stdnum, $password)){

        if (password_verify($password, $response['password'])){ //line 95
            
            $index['id'] = $response['id'];
            $index['username'] = $response['username'];
            $index['student_number'] = $response['student_number'];
            $index['program'] = $response['program'];
            $index['email'] = $response['email'];

            array_push($result['login'], $index);

            $result['success'] = "1";
            $result['message'] = "success";
            echo json_encode($result);
            
            
            }
        else{
                $result['success'] = "0";
                $result['message'] = "error";
                echo json_encode($result);
            }
    }
    else{
        echo "null";
    }

}

型号

function login($stdnum){
    $this->db->where('student_number', $stdnum);
    $query = $this->db->get('mydatabase');
    return $query->row_array();
}

** Postman 结果**

[{"id":"4","username":"qwe","student_number":"213","program":"asd","email":"asd","password":"asd"}]
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message: Undefined index: password</p>
<p>Filename: controllers/api.php</p>
<p>Line Number: 95</p>

<p>Backtrace:</p>
c6ubokkw

c6ubokkw1#

仅将模型中result_array()函数更改为row_array(),因为result_array的结果在array中,而row_array()的结果在object中。

  • 〉使用此模型函数
function login($stdnum){
    $this->db->where('student_number', $stdnum);
    $query = $this->db->get('mydatabase');
    return $query->row_array();
}

相关问题