使用php和mysql只显示当前用户的详细信息

vaj7vani  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(354)

在这部分代码中,score表和profile页都显示了我想要的所有内容,但是它显示了每个人的详细信息,比如
名字姓氏电子邮件类别用户名,但是,我希望它这样,当用户登录时,他们只能看到自己的详细信息
这是score.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
require("db_connect.php");
session_start();
?>

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

<?php
$con=mysqli_connect("localhost","username","Password","Database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");

echo "<table border='1'>
<tr>
<th>ID</th>
<th>Username</th>
<th>Score</th>
<th>Gamedate</th>
<th>QuizTitle</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Username'] . "</td>";
echo "<td>" . $row['Score'] . "</td>";
echo "<td>" . $row['Gamedate'] . "</td>";
echo "<td>" . $row['QuizTitle'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

这是profile.php

<?php
include_once 'db_connect.php';
?>

<!DOCTYPE HTML>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">

    <title>Profile page </title>

 <?php
$con=mysqli_connect("localhost","Username","password","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");

while($row = mysqli_fetch_array($result))
{

        echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
        echo "<b>First name:</b> ". $row['FirstName'];
        echo "<br /><b>Last name:</b> ".$row['Surname'];
        echo "<br /><b>Email:</b> ".$row['Email'];
        echo "<br /><b>Year:</b> ".$row['Username'];
        echo "<br /><b>Date created :</b> ".$row['Date_Creation'];
}

mysqli_close($con);
?>
    </main>

</html>

这些是我尝试运行页面时遇到的错误。在我更改select查询之前,它选择了当前登录的用户,但显示了每个人的信息

ttisahbt

ttisahbt1#

在你的 score.php 文件,在这行:

$result = mysqli_query($con,"SELECT * FROM Score where 'Username' LIKE _['Username']");

看看你的where语句,似乎是错的。
在你的 profile.php ,在这行:

$result = mysqli_query($con,"SELECT `FirstName`,`Surname`,`Email`,`Username`,`Date_Creation`FROM Users ");

你不能传递任何where语句。所以,问题在sql中,特别是在where上。

z18hc3ub

z18hc3ub2#

sql中的单引号用于引用特定字符串,如 'Bob' . 如果要检查列的值,请不要将列名作为 'Username' . 相反,请将列名不带引号: where Username like

yc0p9oo0

yc0p9oo03#

关于错误。您可以使用错误处理来显示查询中的问题。其次,可以使用id获取登录用户的数据信息。您可以通过会话保存用户的id,当用户登录时保存id,然后将其用于where子句。

相关问题