Query mysql username in php

Asked

Viewed 397 times

0

Good afternoon, I am developing a website for my TCC, I am still apprentice in developing sites with php.

I’m trying to get the user to enter your page to receive welcome with your name searched in mysql, follow photo 1.

Foto 1

But I do not want the email to stay, but the name of the user who signed in to your account. First, I created a session to check if the user is logged in to your account.

    <?php 
session_start(); 
if(!isset($_SESSION["email"]) || !isset($_SESSION["senha"])) { 
// Usuário não logado! Redireciona para a página de login 
header("Location: index.php"); 
exit; 
}
?>

Then follows below the H2, showing the welcome.

 <h2>Bem vindo aluno!  <?php echo $_SESSION['email']; ?></h2>

I used a SESSION to call the email just for testing, but I want to call the user name being the attribute "name" in mysql. What easier way can I make it?

Taking into account that when putting the name instead of email, it does not return anything in the welcome message.

NOTE: Adding the PHP code, which makes the query in the database, and had forgotten to add in the question. Follow the code below:

<?php

session_start();
include("conexao.php");

$email = $_POST['email'];
$senha = $_POST['senha'];

$sql_logar = "SELECT * FROM aluno WHERE email = '$email' AND senha = '$senha'";
$exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error());
$num_logar = mysqli_num_rows($exe_logar);

if ($num_logar == 0){

$_SESSION['msg'] = "<div class='alert alert-danger'>Login ou senha incorreto!</div>";
header("Location: index.php?login");
}
else{
//Cria a sessão e verifica tipo de login
while($percorrer = mysqli_fetch_array($exe_logar) ){
$tipo = $percorrer['tipo'];
}

$_SESSION['tipo'] = $tipo;
$_SESSION['email'] = $email;
$_SESSION['senha'] = $senha;
$_SESSION['nome'] = $nome;

if($tipo != "2"){
header("Location: aluno.php");
}else{
header("Location: administrador.php");
}

}
?>
  • Table structure pupil:

    inserir a descrição da imagem aqui

  • https://celke.com.br/artigo/sistema-de-login-com-php-e-mysqli

  • The login system already made friend, the topic is another;

  • @Hermetobermudez , you just want to show the Name instead of the email, right ? Being that, it’s simple. Just you, assign to a $_SESSION['name'] the user name (as you do with the email) and display that $_SESSION['name'] or instead of $_SESSION['email']. Try to edit your question and add to it the query you use to fill out the SESSIONS. So we can visualize and help you in the best way.

  • This Gambi, I thought so, I tried but it didn’t work, because the SESSION I assign in the email is just for verification, how can I check the name of the user who entered.. why if you just put a $_SESSION['name'] in nothing too :/

  • Put in the question the code with the query you fill the SESSION, please.

  • The only query I use is the one I quoted above, then it’s just body with the site, which is the H2 of welcome,

  • Quoted where ? Can’t see, sorry.

Show 3 more comments

1 answer

1


The user did not put the PHP code that performs the query in the database and returns the data that populates the SESSIONS. However, talking in chat, he informed me the code (that he had done) and I added in the question.

  • This made it easier to identify the error.
  • The user was adding the values, which come from the form fields, directly in the SESSIONS, instead of adding the data resulting from the query in the database in the SESSIONS. (Completely wrong, the way I see it).

  • I made below a code to help you achieve your goal. NOTE: I did based on your current need and seeing a way to help you solve your problem.

Below is how the PHP code was:

<?php

    session_start();
    include("conexao.php");

    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $senha = filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING);

    /* 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' AND senha = '$senha'";
    $exe_logar = mysqli_query($conection, $sql_logar) or die (mysqli_error());
    $num_logar = mysqli_num_rows($exe_logar);

    //Verifica se n existe uma linha com o login e a senha digitado
    if ($num_logar == 0){

        $_SESSION['msg'] = "<div class='alert alert-danger'>Login ou senha incorreto!</div>";
        header("Location: index.php?login");
    }
    else{
        //Cria a sessão e verifica tipo de login
        $informacao = mysqli_fetch_assoc($exe_logar);

        $_SESSION['tipo'] = $informacao ['tipo'];
        $_SESSION['email'] = $informacao ['email'];
        $_SESSION['senha'] = $informacao ['senha']; //Não tem necessidade disso
        $_SESSION['nome'] = $informacao ['nome'];


        if($_SESSION['tipo'] != "2"){
            header("Location: aluno.php");
        }else{
        header("Location: administrador.php");
        }
    }
?>
  • The HTML would look like this:

<h2>Bem vindo aluno!
  <?php echo $_SESSION['nome']; ?>
</h2>

  • I hope I’ve helped.
  • 1

    It worked perfectly @Gambi, thanks for the help buddy! And good luck on your tcc ;)

  • Thank you, @Hermetobermudez. Good luck in yours too. May the Force be with you !

  • Ah, and follow the tip @Eliseu B. gave you in the comment upstairs and take a look at that link tutorial. It’s really good.

Browser other questions tagged

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