problem when saving records with fetch - error Undefined index

1

Hi, I have problems within the if (isActive ($ user)) where I want to get the values id, password, id_type, are not saving and I get this message

Notice: Undefined index: id in C:\xampp\htdocs\login_oracle\funcs\funcs.php on line 291

Notice: Undefined index: password in C:\xampp\htdocs\login_oracle\funcs\funcs.php on line 292

Notice: Undefined index: id_tipo in C:\xampp\htdocs\login_oracle\funcs\funcs.php on line 293

Function where the problem is:

function login($usuario, $password)
{
    global db;

    $stmt = $db->prepare("SELECT id FROM usuarios WHERE usuario = :uss OR correo = :uss");
    $stmt->bindValue(":uss",$usuario);
    $stmt->execute();
    $valor= $stmt->fetchColumn(0);

    //printf("<script type='text/javascript'>alert('El valor es : $valor'); </script>");
    if($valor > 0) 
    {
        if(isActivo($usuario))
        {
            printf("<script type='text/javascript'>alert('HOLAA'); </script>");
            $conn = $db->prepare("SELECT id, password, id_tipo FROM usuarios WHERE usuario = :cons OR correo = :cons");
            $conn->execute(array(":cons"=>$usuario));

            $registro = $conn->fetch(PDO::FETCH_ASSOC);

            $a = $registro['id'];
            $b = $registro['password'];
            $c = $registro['id_tipo'];

            $validaPassw = password_verify($password, $b); //password_verify funcion de php, Comprueba que la contraseña coincida con un hash

            printf("<script type='text/javascript'>alert('Los datos son: $a, $b, $c'); </script>");

            if($validaPassw)
            {
                lastSession($id);
                $_SESSION['id_usuario'] = $a;
                $_SESSION['tipo_usuario'] = $c;

                header("location: welcome.php");

            } 
            else 
            {
                $errors = "La contrase&ntilde;a es incorrecta";
            }
            $conn->closeCursor();
        } 
        else 
        {
            $errors = 'El usuario no esta activo';
        }
    } 
    else 
    {
        $errors = "El nombre de usuario o correo electr&oacute;nico no existe";
    }

    $stmt = null;

    return $errors;
}

The connection is:

<?php
    try 
    { 
         $db = new PDO('oci:dbname=localhost', 'TRABAJOFINAL','TRABAJOFINAL'); 

    } 
    catch (Exception $e) 
    {
        die('Error: ' .$e->GetMessage()); 
    }
?>
    
asked by RicardoBarros 13.12.2017 в 00:46
source

2 answers

1

If, as you said in the comment, the var_dump returns this:

array(3) { 
            ["ID"]=> string(2) "12" 
            ["PASSWORD"]=> string(60) "$2y$10$*****" 
            ["ID_TIPO"]=> string(1) "2" 
        } 

you should know that fetch(PDO::FETCH_ASSOC) creates an associative array whose keys are the column names of the database.

The var_dump shows that in your database the column names are written in uppercase. In PHP the key names in the arrays are case sensitive , that is, that ID and id is not the same for an array key. Therefore, you must change the code like this:

//...código

        $registro = $conn->fetch(PDO::FETCH_ASSOC);

        $a = $registro['ID'];
        $b = $registro['PASSWORD'];
        $c = $registro['ID_TIPO'];

//... código

Note:

If you expect more than one row in the results of the query, you must open a loop to read each row of results. The way you have it now will only read the first row.

    
answered by 13.12.2017 / 01:22
source
1

try this:

$a = $registro['**ID**'];
$b = $registro['**PASSWORD**'];
$c = $registro['**ID_TIPO**'];

that way it should work.

    
answered by 13.12.2017 в 01:22