ds3464 2015-01-18 01:20
浏览 9
已采纳

搜索已登录用户的名称

Well I've got a website, in the header of this website I've got two links, one to login and another one to register. I want that, when the user is logged in, those two links disappear, so i can have one saying "Hello " followed by a link to the control panel. like "Hello . . The thing is that it isn't searching the name of the user. It just doesn't show the name. The user is logged with the email and password, and the function usuarioEstaLogado() is the one that confirm the user is logged. Here's the codes:

   function nomeUsuario($conexao){
        $usuarios = array();
        $query = "select nome from usuarios where email = '{$mail}'";
        $resultado = mysqli_query($conexao, $query);
        while($usuario = mysqli_fetch_assoc($resultado)){
            array_push($usuarios, $usuario);
        }
        return $usuarios;
    }


<?php
session_start();
function usuarioEstaLogado() {
    return isset($_SESSION["usuario_logado"]);
}

<div class="container container-twelve">

        <header>
            <div class="twelve columns top remove-bottom">
                <div class="three columns offset-by-nine">
                    <?php 
                        if(usuarioEstaLogado()){
                            $usr = nomeUsuario($conexao);
                            echo" <p>Ola " . $usr['nome'];
                            "<a href='admin'> Painel de Controle</a></p>";
                        }
                    ?>
                    <p><a href="cadastro.php">Cadastre-se</a> | 
                    <a href="login.php">Login <span class="icon-user"></span></a></p>
                </div>
  • 写回答

1条回答 默认 最新

  • dpd46554 2015-01-18 01:24
    关注

    In your top function

        $query = "select nome from usuarios where email = '{$mail}'";
    

    $mail is undefined, you haven't pulled it from global, nor passed it to the nomeUsario function, so that query probably doesn't return anything.

    Your nome function should probably accept an email. Additionally, you probably want the first match in the db, not all of them, like:

    function nomeUsuario($conexao, $mail) {
        $query = "select nome from usuarios where email = '{$mail}'";
        $resultado = mysqli_query($conexao, $query);
        if ($usuario = mysqli_fetch_assoc($resultado)) {
            return $usuario;
        } else {
            return false;
        }
    }
    

    and when you call it, you need the email address, which I am *guessing* is in the $_SESSION?

    $usr = nomeUsuario($conexao,$_SESSION['usuario_email']); //or whatever the name may be :-)
    echo" <p>Ola " . $usr['nome'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?