Print table value

0

I have a login system, which each user is redirected to a specific page. In my database table I have the columns name, user, password and level.

I would like when the user logs in, the name registered in the database appears on your welcome page. The echo part has already been solved and is already printed on the screen, but I can only with the variable usuario , the data in the nome column can not print on the screen.

This is my file that validates the login:

<?php 
require ("db.php");

$usuario = $_POST['inputUsuario'];
$senha = md5($_POST['inputPassword']);

$query = mysqli_query($conn,"SELECT * FROM usuarios WHERE usuario = '$usuario' AND senha = '$senha'");
$row = mysqli_num_rows($query);

$dados = $query->fetch_array();

if ($row > 0){
if($dados['nivel'] == 1){
    session_start();
    $_SESSION['usuario'] = $_POST['inputUsuario'];
    $_SESSION['senha'] = $_POST['inputPassword'];
    header('Location: usuarios/usuario1.php');
}else if($dados['nivel'] == 2){
session_start();
    $_SESSION['usuario'] = $_POST['inputUsuario'];
    $_SESSION['senha'] = $_POST['inputPassword'];
    header('Location: usuarios/usuario2.php');
}else if($dados['nivel'] == 3){
session_start();
    $_SESSION['usuario'] = $_POST['inputUsuario'];
    $_SESSION['senha'] = $_POST['inputPassword'];
    header('Location: usuarios/usuario3.php');
}

}else{
    header('Location: index.php?msg=1');
}
?>

Location where you are trying to print the name:

        <h2><?php echo "Bem vindo ". $_SESSION['nome'];?></h2>
    
asked by anonymous 14.10.2017 / 18:33

1 answer

0

The nome field in $_SESSION was not assigned on the login validation page, so it will not work. Construct this field based on what comes from the table in $row["nome"] :

if ($row > 0){
    session_start();
    $_SESSION['usuario'] = $_POST['inputUsuario'];
    $_SESSION['senha'] = $_POST['inputPassword'];
    $_SESSION['nome'] = $dados['nome']; //assumi aqui o campo se chama nome

    if($dados['nivel'] == 1){
        header('Location: usuarios/usuario1.php');
    } else if($dados['nivel'] == 2){
        header('Location: usuarios/usuario2.php');
    } else if($dados['nivel'] == 3){
        header('Location: usuarios/usuario3.php');
    }
}

Notice also how at the same time I ended up separating the instructions that were common to all cases of ifs making the code simpler and more organized. Now it is only within the ifs the only instruction that is different, the header() .

    
15.10.2017 / 15:18