Trying to make the login work, but shows no error but also does not enter

Asked

Viewed 381 times

2

Like I did the right php as my teacher passed, I changed all the names and it’s all right, I already checked, but when I log in, he doesn’t log in, he just updates the page and doesn’t redirect to the person’s profile page as it is to be done.

The code is this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include ('config.php');
session_start();

if ($_POST['botao']=="Entrar"){

$query = "SELECT * FROM usuarios WHERE email = '".$_POST['email']."' AND senha = '".$_POST['senha']."' ";
$result = mysql_query($query) or print mysql_error();

if(mysql_num_rows($result)>0){

    while ($coluna=mysql_fetch_array($result)) {

        $_SESSION["id_usuario"]= $coluna["id"]; 
        $_SESSION["email"] = $coluna["email"]; 
        $_SESSION["senha"] = $coluna["senha"]; 
        $_SESSION["UsuarioNivel"] = $coluna["nivel"];

        if($coluna['nivel'] == "USER"){ 
            header("Location: perfil.php"); 
            exit; 
        } 

        elseif($coluna['nivel'] == "ADM"){ 
            header("Location: restrita.php"); 
            exit;
        } 

        else {
            echo "Nenhum nível selecionado!";   
        }
        // ----------------------------------------------
    }
} else {
    echo "Login ou Senha Incorreto";    
}

}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="cadastro" >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<style type="text/css">
</style>

<body>
<table border="0" class="banner"><tr><td colspan="4" align="center"><img src="banner.jpg" height="250" width="900"/></td></tr></table>
<br>
<table border="0" align="center" width="910">
    <tr class="link">
        <td> <a href="principal.php"> Home </a> </td>
        <td> <a href="perfil.php"> Meu perfil </a> </td> 
        <td> <a href="login.php"> Entrar </a> </td> 
        <td> <a href="login_funcionario.php"> Área restrita </a> </td> 
    </tr>
</table>
<br /> <br />
<table border="0" align="center">
<tr> <td colspan=3 align=center class="titulo"> Login </td> </tr>
<tr> <td class="texto"> Email: </td> <td colspan="2"> <input type="text" name="email" maxlength="30" class="input"> </td> </tr>
<tr> <td class="texto"> Senha: </td> <td colspan="2"> <input type="password" name="senha" maxlength="20" class="input"></td></tr>
<tr> <td colspan=2 align=right> 
    <input type="submit" name=botao id="botao" value="Entrar" class="botao">
    <input type="submit" name=botao value="Esqueci a senha" class="botao2">
</td> </tr>
<tr> <td colspan=2 align=center>
    <br />
    <font class="texto"> Não é cadastrado? </font>
    <br />
    <font class="link"> <a href="cadastro.php"> Cadastre-se aqui </a>           </font>
</td>
</tr>

resultados

estrutura

  • Your teacher recommended using @?, is sending by post?

  • if you take the @ it gives news, but does not change in much, still not working and yes, by post

  • Edit the question and add the form shipping, you need to treat this notice, usually an if with isset() and the value check already resolves. Related:Why do they say using @arroba to suppress errors is a bad practice?

  • 2

    Play the line <form ... down the tag <body> remember to close the tag form in the end.

  • session_start(); should be the first thing in your php file. keep that in mind! Nor is it good policy to get the form before the head, alias, had never seen it like this.

  • Another note, you are buying an encrypted password with text, this will always give authentication error. which encryption method was used for the password?

Show 1 more comment

2 answers

1

Modify the parameter enctype of your form:

enctype="multipart/form-data"

It is he who is causing the error because with this parameter you are saying that you are uploading a file. Do so:

enctype="application/x-www-form-urlencoded"
  • erased and keeps giving the same thing

  • Instead of deleting put the way I edited in the answer. So it works for me.

  • didn’t work out the same way

  • Hi, Diego, you don’t need to use @username within the questions/answers. This only works in the comments and the Questioner is always notified of new answers.

0

Well, I would do a little different. For conditions to run and show a possible error, even if it is not from Mysql:

session_start();
if ($_POST['botao']=="Entrar"){

    $query = "SELECT * FROM usuarios WHERE email = '".$_POST['email']."' AND senha = '".$_POST['senha']."' ";
    $result = mysql_query($query) or print mysql_error();

    if(mysql_num_rows($result)>0){

        while ($coluna=mysql_fetch_array($result)) {

            $_SESSION["id_usuario"]= $coluna["id"]; 
            $_SESSION["email"] = $coluna["email"]; 
            $_SESSION["senha"] = $coluna["senha"]; 
            $_SESSION["UsuarioNivel"] = $coluna["nivel"];

            if($coluna['nivel'] == "USER"){ 
                header("Location: perfil.php"); 
                exit; 
            } elseif($coluna['nivel'] == "ADM"){ 
                header("Location: restrita.php"); 
                exit; 
            } else {
                echo "Nenhum nível selecionado!";   
            }
            // ----------------------------------------------
        }
    } else {
        echo "Login ou Senha Incorreto";    
    }

}

Observing:

The upload button should have the name="knob" id="knob", so if() can identify. And your form should contain method="post" enctype="Multipart/form-data"

In HTML you can do:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<style type="text/css">
</style>

<body>
<table border="0" class="banner"><tr><td colspan="4" align="center"><img src="banner.jpg" height="250" width="900"/></td></tr></table>
<br>
<table border="0" align="center" width="910">
    <tr class="link">
        <td> <a href="principal.php"> Home </a> </td>
        <td> <a href="perfil.php"> Meu perfil </a> </td> 
        <td> <a href="login.php"> Entrar </a> </td> 
        <td> <a href="login_funcionario.php"> Área restrita </a> </td> 
    </tr>
</table>
<br /> <br />
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="cadastro" >
<table border="0" align="center">
<tr> <td colspan=3 align=center class="titulo"> Login </td> </tr>
<tr> <td class="texto"> Email: </td> <td colspan="2"> <input type="text" name="email" maxlength="30" class="input"> </td> </tr>
<tr> <td class="texto"> Senha: </td> <td colspan="2"> <input type="password" name="senha" maxlength="20" class="input"></td></tr>
<tr> <td colspan=2 align=right> 
    <input type="submit" name=botao id="botao" value="Entrar" class="botao">
    <input type="submit" name=botao value="Esqueci a senha" class="botao2">
</td> </tr>
<tr> <td colspan=2 align=center>
    <br />
    <font class="texto"> Não é cadastrado? </font>
    <br />
    <font class="link"> <a href="cadastro.php"> Cadastre-se aqui </a>           </font>
</td>
</tr>
</table>
</form>
  • 2

    @Alinis and André, I removed the comments here so the page does not get polluted. Please gather in the question and answer information that you have exchanged and is relevant. Thank you!

Browser other questions tagged

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