【问题标题】:How to show specific div based on database value?如何根据数据库值显示特定的 div?
【发布时间】: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>

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这些是 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);
        }
    }
    

    【讨论】:

    • 您好 Tajni,感谢您的帮助。我相信我快到了:) 如果我尝试你的代码,什么也不会发生。无论值是 0 还是 1,div 都保持隐藏状态。
    • var_dump($workout_1) 在这两种情况下输出什么?您是否将 PHP 代码包含在视图文件中(使用 html)?
    • 在这两种情况下都为空。正如您所建议的,我在authenticate.php 中编辑了贵重物品。但我也必须以某种方式将它包含在dashboard.php 中?
    • 是的,需要在dashboard.php 中再次检索您的查询。我将此代码包含在答案中。您应该考虑创建可以包含到两个文件中的通用文件。
    • 在 mysql 中使用正确的 id 运行 SELECT workout_1 FROM users WHERE id = ? 会得到什么?
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多