我似乎无法显示当前登录用户的信息。我不确定我在这里做错了什么。我的登录页面和注销功能正常工作,当我注册用户时,它将该用户添加到数据库中,但我希望能够在登录时显示当前用户数据。
<?php
session_start();
// connects to the database
$mysqli = new mysqli("localhost", "root", "");
$query = "SELECT firstName, lastName, username, password, loyaltyPoints FROM user WHERE username = '".$_SESSION['username']."'";
if($result = $mysqli->query($query))
{
while($row = $result->fetch_assoc())
{
echo $row['firstName'];
echo $row['lastName'];
echo $row['username'];
echo $row['password'];
echo $row['loyaltyPoints'];
}
$result->free();
}
else
{
echo "No results found";
}
?>
<html>
<head>
</head>
<body>
Welcome! Click here to <a href="logout.php" tite="Logout">Logout.</a>
or here to go to the home page --> <a href ="INDEX.html">HOME</a>
</body>
</html>
答案 0 :(得分:1)
首先,您还没有选择任何数据库。先做。
您当前的代码:
$mysqli = new mysqli("localhost", "root", ""); // missing fourth parameter
在那里添加。
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
session_start();
// connects to the database
$mysqli = new mysqli('localhost', 'root', '', 'DATABASE NAME'); // add the database name (fourth parameter)
if(empty($_SESSION['username'])) {
echo 'not logged in';
exit;
}
$query = "SELECT firstName, lastName, username, password, loyaltyPoints FROM user WHERE username = '{$_SESSION['username']}'";
$result = $mysqli->query($query) or die($mysqli->error);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// echo $row['firstName'];
// echo $row['lastName'];
// echo $row['username'];
// echo $row['password'];
// echo $row['loyaltyPoints'];
foreach($row as $val) {
echo "$val <br/>";
}
}
}
else {
echo 'not found';
}
?>
<html>
<head>
</head>
<body>
Welcome! Click here to <a href="logout.php" tite="Logout">Logout.</a>
or here to go to the home page --> <a href ="INDEX.html">HOME</a>
</body>
</html>