Print table value

Asked

Viewed 84 times

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 logged in, the registered name appeared in the database on your welcome page. The part of echo already solved and is already printed on the screen, but only with the variable usuario, the data in the column nome I can’t print on screen.

This is my log-in validation file:

<?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');
}
?>

Where to try to print the name:

        <h2><?php echo "Bem vindo ". $_SESSION['nome'];?></h2>
  • As an aside and not directly focused on the question, consider protecting your page against mysql Injection Attacks, validating input data or using Prepared statements. Where is the echo ? Put this code in too

  • but where is the error? I’m not seeing you pass the name column anywhere! If possible, put the part code where you are trying to print the data

  • Thanks for the tip Diego, I tried to pass the name column data using mysqli_fetch_assoc and did not give. The way the code looks, how could I include a variable for the name column? Sorry for the questions, but this is my first file that I work with php and database, and I have virtually no experience at all.

1 answer

0

On the ground nome in $_SESSION was not assigned on the login validation page, so it will not work. Build this field based on what comes from the $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’s just inside the ifs the only instruction that is different, the header().

  • Please avoid long discussions in the comments; your talk was moved to the chat

  • @Maniero Thank you for the warning, indeed it has spread a lot and even unnecessarily.

Browser other questions tagged

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