PHP: $row undefined variable [closed]

2019-08-22 04:37发布

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条回答
做自己的国王
2楼-- · 2019-08-22 05:09

I think the Problem is that you look for isset($row). Cause the row will allways set on post BUT if there is a entrie in database you will get a row ELSE you will get false.

and then you try to access to "false" like an array.

查看更多
贼婆χ
3楼-- · 2019-08-22 05:11

Use $_SESSION instead of $row, and be careful with indexes, as in $_SESSION array you have lowercase : $_SESSION['lastname'] $_SESSION['email'] and so on, - array keys are case sensitive.

查看更多
We Are One
4楼-- · 2019-08-22 05:11

use $_SESSION instead of $row.

查看更多
Bombasti
5楼-- · 2019-08-22 05:13

I think the problem here is you need to start the session before you check that a session id is set, try starting the session then validating the session variables after:

session_start();

if (!isset($_SESSION['username'])) {
    header("Location: login.php");
}
查看更多
做自己的国王
6楼-- · 2019-08-22 05:32

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.

查看更多
登录 后发表回答