display the user who is logging in PHP

Asked

Viewed 293 times

0

I have a problem, that when the user logs in, correct, shows a div that displays the message "Welcome" ( and I would like you to show the username ) and it does not show, I would also like that if this error showed a div similar only to the message "Fill in the fields correctly".

Follow the PHP code:

<?php
    if(isset($_GET('acao'])){
        if(!isset($_POST['logar'])){
        $acao = $_GET['acao'];
        if(acao=='negado'){
            echo '<strong>Erro!</strong> Você precisa estar logado';
        }
        }
    }
    if(isset($_POST['logar'])){
    //recuperar dados do form
    $email = trim(strip_tags($_POST['email']));
    $senha = trim(strip_tags($_POST['senha']));

    //selecionar banco de dados
    $select = "SELECT * FROM usuario WHERE BINARY email=:email AND BINARY senha=:senha";
    try{
        $result = $conexao->prepare($select);
        $row=$result->fetch(PDO::FETCH_ASSOC);
        $usuario= $row['nomeUsuario'];
        $result->bindParam(':email',$email, PDO::PARAM_STR);
        $result->bindParam(':senha',$senha, PDO::PARAM_STR);
        $result->execute();
        $contar=$result->rowCount();
        if($contar>0){
            $email = $_POST['email'];
            $senha = $_POST['senha'];
            $_SESSION['email'] =$email;
            $_SESSION['senha']= $senha;
            echo('
        <div class="row" style="margin-left:-145%;">
            <div id="mensagem">Seja bem vindo <?php echo $usuario; ?></div>
        </div>
            '); // MSG SUCESSO
            //header("Refresh:3, home.php");
        }else{
            echo('
            <div class="row" style="margin-left:-145%;">
                <div id="mensagem2">Preencha os campos corretamente</div>
            </div>
            ');//MSG ERRO
            header ("Refresh:3, index.php");
        }
    }catch(PDOException $error){
        echo "Erro: ". $error->getMessage();
        }
    }
?>
  • 1

    all query data is returning? It is not the cause but it is an error if(acao=='negao'){ It has to be if($acao=='negao'){ missing $

  • Yes, I had already put rsrs and copied the code from another pc that is offline. The login it does correctly, my two doubts are : - How to show the user name in the div of the message "Welcome" - How to show the error div that does not appear when login failed, whether it is wrong user or blank field.

  • escape HTML, as if($count>0) { ? > ... <div>hit</div> <?php } Else { ? > Error <?php } ?>

  • Boy, your code has more error, it’s hard for us to go straight to the point you want, EM(isset($_GET('acao'])){ Short [ You could post the correct PHP code?

2 answers

1

you’re putting the variable inside quotes, separate them, and you don’t need to open the php tag again:

Current:

echo('
        <div class="row" style="margin-left:-145%;">
            <div id="mensagem">Seja bem vindo <?php echo $usuario; ?></div>
        </div>
            '); // MSG SUCESSO

Change to:

echo('
        <div class="row" style="margin-left:-145%;">
            <div id="mensagem">Seja bem vindo ' . ' $usuario ' . '</div>
        </div>
            '); // MSG SUCESSO

0

1 - You are using PHP within PHP, <?php echo $usuario; ?>

echo('
    <div class="row" style="margin-left:-145%;">
        <div id="mensagem">Seja bem vindo <?php echo $usuario; ?></div>
    </div>
        ');

the correct is

 echo("
    <div class=\"row\" style=\"margin-left:-145%;\">
        <div id=\"mensagem\">Seja bem vindo ".$usuario."</div>
    </div>
        ");

2 - that your style="margin-left:-145%;" may be the cause of not showing the div on the screen. See

<div class="row" style="margin-left:-145%;">
        <div id="mensagem">1 - Seja bem vindo Thiago Luiz Ferreira Tavares</div>
    </div>
    <br>
    
    <div class="row" style="margin-left:-145px;">
        <div id="mensagem">2 - Seja bem vindo Thiago Luiz Ferreira Tavares</div>
    </div>
    <br>
    
     <div class="row" style="margin-left:145px;">
        <div id="mensagem">3 -Seja bem vindo Thiago Luiz Ferreira Tavares</div>
    </div>

  • I’ve tried to put it that way and it just gives me the empty return. I don’t know if the syntax of pulling from the database $user is totally correct, I’m kind of layman still with PDO rsrs

  • Mai I asked in the comment upstairs "all query data is returning?"Should there be more errors so let’s look for

  • @Thiagoluizferreiratavares this style="margin-left:-145%;" is the cause of not appearing the div on the screen. In the source code it appears

  • the style="margin-left:-145%" did not affect html in the part of disappearing the div from the screen, the first div of the is welcome also needs it to stay aligned. Fill the div correctly There it does not appear with or without the style, the configuration is not impacting

  • In this case, in order not to keep running around, you better post your application in full use. The code posted is already with errors that in your application do not exist ...

Browser other questions tagged

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