douluanji8752 2018-08-03 21:56
浏览 261
已采纳

MySQL PHP - 获取数据并以HTML格式呈现

I'm just learning PHP and I'd like to do a basic login. Once logged in, I'd like to show basic information from the user (in this example, just the name), but for some reason I'm not getting the name printed. Could you help me please?

<?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>

The login, logout and session are already working. The table structure contains a table named users with the columns: id, username, password, name, email.

Thanks

  • 写回答

2条回答 默认 最新

  • dongtuo3795 2018-08-03 22:14
    关注

    $uname is undefinded

    Try: $_SESSION['uname'] on line 14;

    Alway u can debug this e.g. var_dump($sql_query) and execute it in phpmyadmin

    And if you want use $row['name'], you must have assoc array: $row = mysqli_fetch_assoc($result);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站