gpt4 book ai didi

PHP: $row undefined variable

转载 作者:行者123 更新时间:2023-11-29 11:48:21 24 4
gpt4 key购买 nike

我的问题是我想显示数据库中的用户配置文件的详细信息。由于某种原因,它一直给我错误“ undefined variable :行”。

This is an image of the problem

另外,我可以使用此代码显示用户用户名和用户 ID。 (HTML 代码)
所以我不太确定缺少什么或者我是否做错了什么。
希望你能帮助我。
提前谢谢您:)


PHP 代码 - 数据库连接和 SQL 查询

<?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 代码

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

最佳答案

在 HTML 代码中,您使用 $row['LastName'] ,而它应该是 $_SESSION['lastname'] (其他的也一样)变量)。 $row 仅在处理 POST 数据并执行 SQL 查询时设置,在未提交表单的情况下打开页面时不会设置。

关于PHP: $row undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34557030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com