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条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改