【问题标题】:I am having trouble creating a user profile with php我在使用 php 创建用户配置文件时遇到问题
【发布时间】:2022-11-29 12:09:25
【问题描述】:

我想为大学数据库系统制作一个个人资料页面。但是,我的 php 代码的 sql 查询部分遇到了麻烦。

这是配置文件页面 php:

<?php
    
    include "server.php";
    error_reporting(0);

    session_start();

    if(isset($_SESSION['username']))
    {
        header("location:login.php");
    }

    elseif ($_SESSION['usertype'] == "instructor") {

        header("location:login.php");
    }

    // mysql base configuration
    define('DB_SERVER', 'localhost:3306');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', '');
    define('DB_DATABASE', 'universitydatabse');
    $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        

    $sql = "SELECT * FROM studentview WHERE Student_ID = $_SESSION['username']";

    $result = mysqli_query($db, $sql);
    
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Account page</title>
    <!--Font aesome CDN-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <!-- side navigation -->
    <div class= "vertical-nav-bar" >
        <img src="Logo_small.png" class="logo">
        <a href="#"><i class="fa-regular fa-square"></i>Account</a>
        <a href="#"><i class="fa-regular fa-square"></i>Dashboard</a>
        <a href="#"><i class="fa-solid fa-clipboard-check"></i> Attendance</a>
        <a href="#"><i class="fa-solid fa-table-columns"></i>  Timetable</a>
    </div>


    <!-- Page content -->
    <div>
        
        <!-- header bar -->
        <div class="header-bar">
            <a href="" style="font-size: 45px; padding-left: 130px;">Account</a>

            <div class="logout">
                <a href="logout.php" class="logout-btn">Logout</a>
            </div>
        </div>

        <!-- content -->
        <center>
            
            <div class="container">
                <?php

                    while ($info = $result->fetch_assoc()) 
                    {
                ?>  
                
                <h1>
                    <?php echo "{$info['F_Name']}"; ?>
                </h1>

                <?php

                    }
                ?>
            </div>
            <br>
            <div class="dropdown">
                <button onclick="myFunction()" class="drop-btn">View Grades</button>

                <div id="id02" class="dropdown-content">
                    <br>
                    <table class="drop-table">
                        <tr>
                            <th class="drop-table-head">CRN</th>
                            <th class="drop-table-head">Course Name</th>
                            <th class="drop-table-head">Grade</th>
                            <th class="drop-table-head">Credit Hours</th>
                        </tr>

                        <tr>
                            <td class="drop-table-head" style="font-weight:revert;">SOF_9U</td>
                            <td class="drop-table-head" style="font-weight:revert;">Architecture of Software</td>
                            <td class="drop-table-head" style="font-weight:revert;">A+</td>
                            <td class="drop-table-head" style="font-weight:revert;">3.00</td>
                        </tr>
                    </table>
                    <br>
                    <table class="drop-table">
                        <tr>
                            <th> </th>
                            <th class="drop-table-head" style="font-weight:revert;">Attempt Hours</th>
                            <th class="drop-table-head" style="font-weight:revert;">Passed Hours</th>
                            <th class="drop-table-head" style="font-weight:revert;">GPA</th>
                        </tr>

                        <tr>
                            <th class="drop-table-head">Current Term</th>
                            <td class="drop-table-head" style="font-weight:revert;">6.00</td>
                            <td class="drop-table-head" style="font-weight:revert;">6.00</td>
                            <td class="drop-table-head" style="font-weight:revert;">3.54</td>
                        </tr>
                    </table>
                    <br>
                    <p>Unofficial Transcript</p>
                </div>  
            </div>
        </center>   
    </div>


    <script>
        /* When the user clicks on the button, toggle between hiding and showing the dropdown content */
        function myFunction() {
            document.getElementById("id02").classList.toggle("show");
        }

        /* close the dropdown if the user clicks outside of it */
        window.onclick = function(event) {
            if (!event.target.matches('drop-btn')) {
                var dropdowns = document.getElementByClassName("dropdown-content");
                var i;
                for (i = 0; i < dropdowns.lenth; i++) {
                    var openDropdown = dropdowns[i];
                    if (openDropdown.classList.contains('show')) {
                        openDropdown.classList.remove('show');
                    }
                }
            }
        }
    </script>
</body>
</html>

在这段代码中,我试图只打印登录用户的名字(测试)。当我使用 sql 查询运行代码时:

$sql = "SELECT * FROM studentview WHERE Student_ID = $_SESSION['username']";

我的页面上没有任何输出,它下面的所有 html 代码也没有显示。我使用 $_REQUEST 从登录页面获取输入(它有 POST 请求方法)。它也不起作用。

$_SESSION['username'] 是我在服务器页面中创建的会话,用于验证我们何时运行会话。

当我在没有 WHERE 条件的情况下运行代码时,我会在名字列中获取所有数据,并显示其余的 html 代码。

$sql = "SELECT * FROM studentview;

所以我知道问题出在这部分代码,我只是不知道我做错了什么。

谢谢你。

【问题讨论】:

  • 我们不知道您最早的错误在哪里。您需要提供更好的调试详细信息。您应该使用准备好的语句而不是直接将 php 变量注入您的 SQL 字符串。我们不知道include "server.php"; 内部发生了什么,请检查您的错误日志。 if(isset($_SESSION['username'])) 意味着如果该值已声明且非空,您的脚本将永远不会到达 $sql 声明。你的意思是使用!isset()吗?

标签: php html css mysql database


【解决方案1】:

您确定您的字段 Student_id 或来自 session["username"] 的字段正确吗?

可能你忘了在 $_SESSION 之前用 session_start(); 开始会话

您可以访问此链接,因为它已被讨论

https://stackoverflow.com/a/12514463/18300912

【讨论】:

  • 是的,他们都是正确的。我担心我可能没有使用正确的规范 $_SESSION["username"] 但这是我在登录页面中使用的并且它有效。
猜你喜欢
  • 2016-04-14
  • 2011-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 2016-04-15
  • 2020-04-05
  • 1970-01-01
相关资源
最近更新 更多