PHP/Mysql Undefined variable in code

Asked

Viewed 647 times

0

I’m trying to run this php code, but it reports me an error "Notice: Undefined variable: num_row in C: wamp64 www balance login.php on line 63" The variable was created on line 23 but it still reports the undefined error, thanks in advance!

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title> Saldo</title>
</head>
<?php

if(isset($_POST['entrar']))
{
$username = trim($_POST['login']);
$password = trim($_POST['senha']);
$query = "SELECT login, senha FROM usuarios WHERE login='$_POST[login]'AND senha='$_POST[senha]'";

$result = mysqli_query($conn,$query) or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
if( $num_row == 1 )
     {
 $_SESSION['id']=$row['id'];
 header("Location: home.php");
 exit;
  }
  elseif ($num_row == 0){

  }
}
?>

<body>

    <body>
        <div class="container">
            <div class="row">
                <div class="col-sm-9 col-md-7 col-lg-5 mx-auto">
                    <div class="card card-signin my-5">
                        <div class="card-body">
                            <h5 class="card-title text-center">Entrar</h5>
                            <form class="form-signin" method="POST" action="login.php">
                                <div class="form-label-group">
                                    <input type="email" name="login" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
                                    <label for="inputEmail">Email</label>
                                </div>

                                <div class="form-label-group">
                                    <input type="password" name="senha" id="inputPassword" class="form-control" placeholder="Password" required>
                                    <label for="inputPassword">Senha</label>
                                </div>

                                <div class="custom-control custom-checkbox mb-3">
                                    <input type="checkbox" class="custom-control-input" id="customCheck1">
                                    <label class="custom-control-label" for="customCheck1">Lembrar minha senha</label>
                                </div>
                                <button name="entrar" class="btn btn-lg btn-primary btn-block text-uppercase" type="submit">Continuar</button>
                                <?php 
                                if ($num_row == false){
                                echo "<p align = 'center'><font color ='red'> Senha incorreta </font></p>";
                                }
                                ?>
                                <hr class="my-4">
                                <button class="btn btn-lg btn-secondary btn-block text-uppercase" type="submit"><i class="fab  mr-2"></i> Não possui cadastro? Cadastre-se</button>
                                <button class="btn btn-lg  btn-secondary btn-block text-uppercase" type="submit"><i class=" mr-2"></i> Esqueci minha senha</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</body>

</html>
  • 2

    But line 23 is inside a if. And when the condition of this if is not satisfied?

1 answer

2


Repair that snippet of your code:

<?php

if(isset($_POST['entrar']))
{
$username = trim($_POST['login']);

If $_POST['entrar'] is not defined, this clause will not be met. Thus skipping this whole block of code. The question is that its variable $num_row is being defined within this block. It is a typical form handling problem in the same file...

One way to solve this without missing the structure of your code is to add a check if $num_row is defined there where you "check" if its value is false:

//...
<?php 
if (isset($num_row) && $num_row == false){
echo "<p align = 'center'><font color ='red'> Senha incorreta </font></p>";
}
?>
//...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.