Trying to make login work, but does not show error but does not enter either

2

I did the php right as my teacher passed, I changed all the names and it's alright, I checked, but when I do the login, it does not go in, it only refreshes the page and it does not redirect to the profile page of the person 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>

    
asked by anonymous 14.09.2015 / 21:18

2 answers

1

Modify the parameter enctype of your form:

enctype="multipart/form-data"

It's the one who is causing the error, because with this parameter you are saying that you are uploading a file. Here's how:

enctype="application/x-www-form-urlencoded"
    
15.09.2015 / 00:07
0

Well, I would do it a little differently. 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";    
    }

}

Note:

The submit button should have the name="button" id="button", so that if () can identify. And your form should contain the 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>
    
14.09.2015 / 21:28