PHP:$ row未定义的变量 [英] PHP: $row undefined variable
问题描述
我的问题是我想显示我在我的数据库上的用户配置文件的详细信息。由于某些原因,它会一直显示未定义的变量:row的错误。
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'.
$ b $bÚ
此外,我可以使用此代码显示用户Username和UserID。 (HTML CODE)$ b $bÚ
所以我不知道缺少什么或者我做错了。
希望你能帮助我。
_c
提前感谢:)
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 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
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>
推荐答案
$ c> $ row ['LastName'] 何时应该 $ _ SESSION ['lastname']
。 $ row
仅在处理 POST
数据时设置,并且执行SQL查询,页面打开,不提交表单。
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.
这篇关于PHP:$ row未定义的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!