Home > Software design >  How to solve undefined index error when I want to access a certain column from my database using cod
How to solve undefined index error when I want to access a certain column from my database using cod

Time:09-27

I want to verify the password of the user logging in if it matches the password from my database. However, for some reason, I can't extract that single data even when I try to echo it. (echo $response['password'];) But the json_encode is working as seen in the postman result.

Controller

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

}

Model

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

Postman Result

[{"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>

CodePudding user response:

change only in your model result_array() function to row_array() because result of result_array is in array and row_array() result is in object.

-> use this model function

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