Castle
Castle

Reputation: 427

i cant show if a query result is null or not

Im doing a Login page on my project and i`ve to check if the "username" that i get is on the Data base

i`ve tried to fetch the query and save the results on a variable

// #########login.php############
<html>
    <head>
     <title>Login</title>

    </head>
    <body>
        <form id="form" action="login-action.php" method="post">
            <input type="text" id='usuario' name='usuario' placeholder="Usuario">
            <input type="password" id='password' name='password' placeholder="Contraseña">
            <input type="submit" value="Login">
        </form>
    </body>
</html>
<?php


<?php
// #########login-action.php############
$usuario = $_POST["usuario"];
$password = $_POST["password"];

function comprobarLogin ($dbh, $usuario){
    $data = array("usuario" => $usuario);

    $stmt = $dbh->prepare("SELECT nomUsuario FROM Usuarios Where nomUsuario = :usuario");
    $stmt->setFetchMode(PDO::FETCH_OBJ);
    $stmt->execute();


    $row = $stmt->fetch();

    if($row == null){
        echo $usuario;
        require "login.php";
    }
      else {
        require "index.php";
    }


}
include "conexionBD.php";
$dbh = connect();
comprobarLogin($dbh,$usuario);

?>
   if($row == null){
            echo $usuario;
            require "login.php";
        }
          else {
            require "index.php";
        }

this if never shows the else result even if the user does exist therefore, the variable $row is always null

Upvotes: 0

Views: 65

Answers (1)

Jim Panse
Jim Panse

Reputation: 2270

You have also to use the parameter you already prepared

Replace

$stmt->execute();

with

$stmt->execute($data);

Upvotes: 2

Related Questions