Error with PHP code

Asked

Viewed 49 times

-1

Good evening, I am developing a login system, but the message system when typing incorrect email or password is giving error. Some changes were made in my previous post, but the problem was not solved. The errors shown on the pages are:

 Notice: Undefined index: cdg in C:\xampp\htdocs\tcc\index.php on line 69

 Notice: Undefined variable: msg in C:\xampp\htdocs\tcc\index.php on line 86

Below is the HTML and PHP code with the lines marked as the error shown:

HTML:

<a href="#login" class="btn-get-started"  data-toggle="modal" data-target="#modalExemplo">Cadastre-se</a>
  <div class="modal fade" id="modalExemplo" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Entre</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
                    <?php
  //LINHA 69          $cdg = $_GET['cdg'];
                    if ($cdg == 1) {
                        $msg = "Login ou senha incorreto!";
                    }
                ?>
                <div class="modal-body text-md-center">
                    <form method="post" action="valida.php">
                        <div class="form-row">
                            <div class="form-group">
                                <input type="email" class="form-control" name="email" id="email" placeholder="Email:">
                                <input type="password" class="form-control" id="senha" name="senha" placeholder="Senha:">
                                <br>
                            </div>
                        </div>
                        <input type="submit" name="btnLogin" value="Entrar" class="btn btn-outline-warning"><br><br>
                        <div class='alert alert-danger'>
                            <?php
          //LINHA 86   echo $msg;
                            ?>
                        </div>
                        <p class="text-center">Caso não possuir conta,
                            <a href="#cadastrouser"  data-toggle="modal" data-target="#modalSelecionar" data-dismiss="modal">cadastre-se</a>
                        </p>



                    </form>
                </div>
              </div>
      </div>
    </div>

PHP:

<?php
include("conexao.php");

$email = $_POST['email'];
$senha = $_POST['senha'];
/* Verifica se existe usuario, o segredo ta aqui quando ele procupa uma 
linha q contenha o login e a senha digitada */
$sql_logar = "SELECT * FROM aluno WHERE email = '$email' && senha = '$senha'";
$exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error());
$fet_logar = mysqli_fetch_assoc($exe_logar);
$num_logar = mysqli_num_rows($exe_logar);

//Verifica se n existe uma linha com o login e a senha digitado
if ($num_logar == 0){
    header("Location: index.php#login?cod=1");
} 
else{
   //Cria a sessão e manda pra pagina principal.php
   session_start();
   $_SESSION['email'] = $email;
   $_SESSION['senha'] = $senha;
   header("Location:aluno.php");
}
?>
  • $_GET['cdg'] and $msg does not exist and your code must first check if it exists and then show or recover

  • Opa Virgilio, I would like to take this direct php 'cdg' as login attempt, as if it were num_log, but it did not work

  • Then you need to take what exists! the cdg you’re passing in the url?

  • I’m not, how can I do it in a practical way?

  • I don’t know how you’re doing and what you need but, it’s in the type url http://www/logar.php?cdg=1 then in your PHP code it works. but still you have to check the existence with isset()

  • I managed to get the first error out using an isset, but the second one I could not, when I put the isset, gives an error in the code that does not even open the page.. I need to put a SESSION in php so that when login is wrong it shows the message: $_SESSION['msg'] = "<div class='Alert Alert-Danger'>Incorrect login or password! </div>"; How can I do?

  • What I said above is refente to echo $msg in html

Show 2 more comments

1 answer

0


According to the error, I solved the first and the second, but the message is not displayed to the end user. Follow the updated html part code, solved problem!:

HTML:

<?php
                        if(isset($_GET['cdg'])) {
                        $cdg = $_GET['cdg'];
                        if ($cdg == 1) {
                            $msg = "<div class='alert alert-danger'>Login ou senha incorreto!</div>";
                        }
                      }
                    ?>
                    <div class="modal-body text-md-center">
                        <form method="post" action="valida.php">
                            <div class="form-row">
                                <div class="form-group">
                                    <input type="email" class="form-control" name="email" id="email" placeholder="Email:">
                                    <input type="password" class="form-control" id="senha" name="senha" placeholder="Senha:">
                                    <br>
                                </div>
                            </div>
                            <input type="submit" name="btnLogin" value="Entrar" class="btn btn-outline-warning"><br><br>

                                <?php
                                $msg = (isset($_GET['msg'])) ? $_GET['msg'] : null;
                                echo $msg;
                                ?>

                            <p class="text-center">Caso não possuir conta,
                                <a href="#cadastrouser"  data-toggle="modal" data-target="#modalSelecionar" data-dismiss="modal">cadastre-se</a>
                            </p>

Browser other questions tagged

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