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'.
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>
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.
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.
use
$_SESSION
instead of$row
.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:
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 processingPOST
data and you do the SQL query, it's not set when the page is opened without submitting a form.