我正在学习PHP,并且想进行基本的登录。登录后,我想显示用户的基本信息(在此示例中,仅显示名称),但是由于某些原因,我没有显示名称。请问你能帮帮我吗?

<?php
include "config.php";

// Session
if(!isset($_SESSION['uname'])){
    header('Location: login.php');
}
// Logout
if(isset($_POST['but_logout'])){
    session_destroy();
    header('Location: login.php');
}
// CHECK THIS
$sql_query = "select * from users where username='".$uname."'";
$result = mysqli_query($con,$sql_query);
$row = mysqli_fetch_array($result);
?>

<!doctype html>

<html>
    <head></head>
    <body>
        <form method='post' action="">
            <h1>Dashboard</h1>
            <div>
                <!-- CHECK THIS -->
                <h2>Hello <?php echo $row['name']; ?></h2>
            </div>
            <div>
                <input type="submit" value="Logout" name="but_logout">
            </div>
        </form>
    </body>
</html>


登录,注销和会话已在工作。
该表结构包含一个名为users的表,其列为:id,用户名,密码,名称,电子邮件。

谢谢

最佳答案

$uname未定义
尝试:在第14行的$_SESSION['uname']
总是可以调试此例如var_dump($ sql query)并在phpmyadmin中执行
如果要使用$row['name'],则必须具有assoc数组:$row = mysqli_fetch_assoc($result);

07-24 09:43
查看更多