dongsi1954 2016-01-01 14:33
浏览 56
已采纳

PHP:$ row undefined变量[关闭]

My problem is I want to show the details of a user profile that I have on my database. For some reason, it keeps giving me errors of 'Undefined variable: row'.

This is an image of the problem

Also, I can show the user Username and UserID using this code. (HTML CODE)
So I'm not really sure what's missing or if I did something wrong.
Hope that you could help me.
THANK YOU in advance :)


PHP CODE - DB CONNECTION & SQL QUERY

<?php
$id = session_id();
if ($id == "") {
    session_start();
}
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
} //redirects to the login page if the user is not logged in

if($_POST){       
    try {
        $host = '127.0.0.1';
        $dbname = 'webdev_2014376';
        $user = 'root';
        $pass = '';
        # MySQL with PDO_MYSQL
        $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    } catch(PDOException $e) {echo 'Error';}   

    ///////////////////////////////////////////////////////////////////////////////

    $sqlQuery = "SELECT * FROM users WHERE UserID = :UserID LIMIT 1";            
    $statement = $DBH->prepare($sqlQuery);    
    $status = $statement->execute();

    if (!$status) {
        die("Could not retrieve user");
    }$row = $sqlQuery->fetch(PDO::FETCH_ASSOC);

    if(isset($row)){
        $_SESSION['id'] = $row['UserID'];  
        $_SESSION['username'] = $row['Username'];
        $_SESSION['firstname'] = $row['FirstName']; 
        $_SESSION['lastname'] = $row['LastName'];        
        $_SESSION['email'] = $row['Email'];        
        $_SESSION['birthday'] = $row['Birthday'];       
        $_SESSION['phonenumber'] = $row['PhoneNumber'];
    }      
}   

?>


HTML CODE

    <body>
        <div class="container container_12">
            <div class="header grid_12">
                <div class="headerTitle grid_6 alpha">
                    <h1>Facebook Again!</h1>
                </div>

                <?php
                if (isset($_SESSION['username'])) {
                    echo '<div class="headerNav grid_6 omega">';
                    echo '<ul>';
                    echo '<li class="grid_1 alpha"><a href="index.php">Home</a></li>';
                    echo '<li class="grid_2 push_1"><a href="profile.php?UserID='.($_SESSION['id']).'">'. ($_SESSION['username']) . '</a></li>';
                    //echo '<li class="grid_2 push_1"><a href="profile.php?UserID=11">' . ($_SESSION['Username']) . '</a></li>';
                    echo '<li class="grid_2 omega push_1"><a href="logout.php"><span class="glyphicon glyphicon-log-in"></span> Logout</a></li>';
                    echo '</ul>';
                    echo '</div>';
                }
                else {
                    echo '<div class="headerNav grid_6 omega">';
                    echo '<ul>';
                    echo '<li class="grid_2 alpha"><a href="login.php">Login</a></li>';
                    echo '<li class="grid_2 omega"><a href="register.php">Register</a></li>';
                    echo '</ul>';
                    echo '</div>';
                }
                ?>

            </div>
            <div class="profile grid_12">
                <div class="profPic  grid_3 suffix_1 alpha">
                    <img src="images/profile/RTZ.jpg" style="width: 300px; height: 300px;">               
                </div>
                <div class="profText prefix_1 grid_7 omega">
                    <?php

                        echo "<h1>Profile of " . $_SESSION['username'] . "</h1>";

                            echo '<tr>';
                            echo '<td class="vedHeaders">First Name: </td>' . '<td class="vedOutput">' . $_SESSION['firstname'] . '</td><br/>';
                            echo '</tr>';
                            echo '<tr>';
                            echo '<td class="vedHeaders">Last Name: </td>' . '<td class="vedOutput">' . $row['LastName'] . '</td><br/>';
                            echo '</tr>';
                            echo '<tr>';
                            echo '<td class="vedHeaders">Email: </td>' . '<td class="vedOutput">' . $row['Email'] . '</td><br/>';
                            echo '</tr>';
                            echo '<tr>';
                            echo '<td class="vedHeaders">Birthday: </td>' . '<td class="vedOutput">' . $row['Birthday'] . '</td><br/>';
                            echo '</tr>';
                            echo '<tr>';
                            echo '<td class="vedHeaders">Phone Number: </td>' . '<td class="vedOutput">' . $row['PhoneNumber'] . '</td><br/>';
                            echo '</tr>';
                    ?>
                </div>
            </div>

    </body>
  • 写回答

5条回答 默认 最新

  • douzhuo5671 2016-01-01 14:39
    关注

    In the HTML code you're using $row['LastName'] when it should be $_SESSION['lastname'] (and the same for the other variables). $row is only set when you're processing POST data and you do the SQL query, it's not set when the page is opened without submitting a form.

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

报告相同问题?

悬赏问题

  • ¥15 使用yolov5-7.0目标检测报错
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备