【发布时间】:2020-05-30 09:10:43
【问题描述】:
我是 PHP 和 MySQL 的新手。我想要一个简单的网站,用户可以在其中登录到一个简单的仪表板。 (没有人可以在这个网站上注册,它只适用于我的客户,我给了他们登录详细信息。是的,我知道密码应该存储散列,但在这种情况下我不需要它。)
在仪表板中,我想根据数据库值显示特定的 div。比如users表中workout_1的行是true,我想显示divworkout_1等,如果值为false,则div应该隐藏。
根据一些教程,我想出了如何设置一个简单的登录/注销过程。现在我被困在如何设置这个显示/隐藏 div 程序上。我所知道的是我必须以某种方式获取查询,但正如我所说,我是 PHP 和 MySQL 的新手,我非常感谢你的一些建议。
这是 authentication.php
<?php
session_start();
$DATABASE_HOST = '**';
$DATABASE_USER = '**';
$DATABASE_PASS = '**';
$DATABASE_NAME = 'tablename';
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
exit('Error: ' . mysqli_connect_error());
}
if ( !isset($_POST['username'], $_POST['password']) ) {
exit('Please fill both the username and password fields!');
}
if ($stmt = $con->prepare('SELECT id, password FROM users WHERE username = ?')) {
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
if ($_POST['password'] === $password) {
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
header('Location: ../dashboard.php');
} else {
echo 'Incorrect password!';
}
} else {
echo 'Incorrect username!';
}
$stmt->close();
}
这是dashboard.php
<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
header('Location: index.php');
exit;
}
?>
这是我想根据 MySQL 查询显示/隐藏的 HTML。
<div id="workout_1">There are some notes for users they attended the 1st workout.</div>
<div id="workout_2">There are some notes for users they attended the 2nd workout.</div>
<div id="workout_3">There are some notes for users they attended the 3rd workout.</div>
【问题讨论】: