Why do I get this error?

0

I am making a validation in the Login and I get this error

  

Notice: Undefined variable: connection in   C: \ xampp \ htdocs \ Inventory Infrastructure \ php \ validar.php on line 14

     

Fatal error: Uncaught Error: Call to a member function prepare () on   null in C: \ xampp \ htdocs \ Inventory Infrastructure \ php \ validar.php: 14   Stack trace: # 0 {main} thrown in   C: \ xampp \ htdocs \ Inventory Infrastructure \ php \ validar.php on line 14

Someone can help me, I'm just starting with PHP programming and it's the first time I've faced this. I leave the source code.

session_start();
if(isset($_SESSION['usuario'])){
    header('location: ../index.php');
}

if ($_SERVER['REQUEST_METHOD']=='POST') {
    $usuario = $_POST['user'];
    $password = $_POST['pass'];

    require('conexion.php');

    $consulta = $conexion -> prepare('SELECT * FROM usuarios WHERE Nombre_Usuario= :user AND Contrasena= :pass');
    $consulta ->execute(array(':user'=>$usuario, ':pass'=>$password));

    $resultado = $consulta -> fecth();
    if($resultado!==false){
        $_SESSION['usuario'] = $usuario;
        header('location: index.php');
    }else{
        header('location: login.php');
    }
}

Thanks

    
asked by Leo Porras 30.07.2018 в 18:05
source

1 answer

0

Why do not you try to declare the connection to the database after starting the session? so

session_start();
require('conexion.php');
if(isset($_SESSION['usuario'])){
    header('location: ../index.php');
}

if ($_SERVER['REQUEST_METHOD']=='POST') {
    $usuario = $_POST['user'];
    $password = $_POST['pass'];



    $consulta = $conexion -> prepare('SELECT * FROM usuarios WHERE Nombre_Usuario= :user AND Contrasena= :pass');
    $consulta ->execute(array(':user'=>$usuario, ':pass'=>$password));

    $resultado = $consulta -> fecth();
    if($resultado!==false){
        $_SESSION['usuario'] = $usuario;
        header('location: index.php');
    }else{
        header('location: login.php');
    }
}
    
answered by 30.07.2018 в 18:50