我有一个包含一些列的数据库,其中两列是用户名和密码,其他列是来自客户的信息...登录后,我很难从除用户名之外的其他列中检索数据。 使用下面的代码,我只能在仪表板中显示用户名。我应该如何继续才能检索其他列中的信息? 谢谢你的帮助!!
登录页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>LSC</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
require('db.php');
session_start();
// When form submitted, check and create user session.
if (isset($_POST['username'])) {
$username = stripslashes($_REQUEST['username']); // removes backslashes
$username = mysqli_real_escape_string($con, $username);
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con, $password);
// Check user is exist in the database
$query = "SELECT * FROM `users` WHERE username='$username'
AND password='$password'";
$result = mysqli_query($con, $query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$_SESSION['username'] = $username;
// Redirect to user dashboard page
header("Location: dashboard.php");
} else {
echo "<div class='form'>
<h3>Incorrect Username/password.</h3><br/>
<p class='link'>Click here to <a href='login.php'>Login</a> again.</p>
</div>";
}
} else {
?>
<form class="forminicial" method="post" name="login">
<h1 class="login-title">LSC</h1>
<input type="text" class="login-input" name="username" placeholder="ID da denúncia" autofocus="true"/>
<input type="password" class="login-input" name="password" placeholder="Palavra-Passe"/>
<input type="submit" value="Entrar" name="submit" class="login-button"/>
<p class="link">...</p>
</form>
<?php
}
?>
</body>
</html>
仪表板
<?php
//include auth_session.php file on all user panel pages
include("auth_session.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LSC</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="form">
<h1 class="login-title">LSC</h1>
<p>ID de Denúncia: <?php echo $_SESSION['username']; ?>!</p>
<p>Categoria da Denúncia:</p>
<p><a href="../index.php">Sair</a></p>
</div>
</body>
</html>
AUTH_SESSION
<?php
session_start();
if(!isset($_SESSION["username"])) {
header("Location: login.php");
exit();
}
?>
如果您想从数据库中检索其他信息并将其存储在会话中供以后使用,则需要在用户登录时从数据库中获取该信息并将其存储在会话中。
请按照如下所述修改您的登录页面。
登录页面
if ($rows == 1) {
$userData = mysqli_fetch_assoc($result);
$_SESSION['username'] = $userData['username'];
$_SESSION['user_id'] = $userData['user_id']; // Assuming 'user_id' is a column in your database
header("Location: dashboard.php");
} else {
//Your existing code
}
在仪表板页面中,您可以检索并显示此附加信息:
仪表板
<?php
include("auth_session.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LSC</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="form">
<h1 class="login-title">LSC</h1>
<p>ID de Denúncia: <?php echo $_SESSION['user_id']; ?>!</p>
<!-- Add similar lines for other columns -->
<p>Categoria da Denúncia:</p>
<p><a href="../index.php">Sair</a></p>
</div>
</body>
</html>
根据您的数据库架构和要检索并在仪表板中显示的列修改代码。请注意,当我准备您的解决方案时,我假设您的数据库中有一个名为
user_id
的列。