php - 如何根据数据库值显示特定的 div?
问题描述
我是 PHP 和 MySQL 的新手。我想要一个简单的网站,用户可以在其中登录到一个简单的仪表板。(没有人可以在这个网站上注册,它只适用于我的客户,我给了他们登录详细信息。是的,我知道密码应该存储散列,但在这种情况下我不需要它。)
在仪表板中,我想根据数据库值显示特定的 div。例如,如果workout_1
表格中的行users
是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>
解决方案
这些是 MySQL 的基础知识,我的第一个建议是查看有关 SO 的其他类似问题并阅读有关将 MYSQL 连接到 PHP 的信息。
您需要更改一些查询并workout_1
从表用户那里获取价值。
$stmt = $con->prepare('SELECT id, password, workout_1 password FROM users WHERE username = ?')
并将此参数的值绑定到变量。
$stmt->bind_result($id, $password, $workout_1);
当您有合适的变量时,您可以在代码中的适当位置制作条件语句。
<?
if((bool)$workout_1) {
echo'<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>
数据库中的布尔值(真或假)应存储在整数类型的列中(1 - 表示真,0 - 表示假)。此外,您应该避免将 PHP 代码与 View 层 (HTML) 混合,但一开始没关系。
编辑:第二种方法。包含在dashboard.php 中。
$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 ($stmt = $con->prepare('SELECT workout_1 FROM users WHERE id = ?')) {
$stmt->bind_param('i', $_SESSION['id']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($workout_1);
}
}
推荐阅读
- angularjs - 部署到服务器(GCP-AppEngine)后,无法在 AngularJs 中加载模块。错误:[$injector:unpr]
- r - 如何使用 data.table 找到组之间的线性回归?
- javascript - 如何根据一个条件返回两个值?
- tensorflow - 使用混淆矩阵值的 TensorFlow 自定义指标
- python - KERAS 在 docker 容器中添加第一层时随机卡住
- reactjs - Button doesn't disappear immediately; need to move mouse during animation using clip-path
- rabbitmq - spring.rabbitmq.addresses :负载均衡器地址与节点地址列表
- windows - 为管理员安装了不同版本的 npm;如何更新本地版本?
- android - 带有 *multiple* 的 Android 视图绑定
可能的? - javascript - 在 Javascript 中显示整个类,而不是运行到 ~100 个字符的部分文本(SVELTE,但可能更通用)