【问题标题】:Retrieving username from MYSQL database [closed]从MYSQL数据库中检索用户名[关闭]
【发布时间】:2017-05-04 09:32:35
【问题描述】:

已回答的问题:

所以我已经为我的网站添加了登录和注册功能,但现在我会 喜欢将用户名和排名添加到侧边栏..但我不能 弄清楚如何做到这一点,这就是我向你寻求帮助的原因。 Signup.inc.php:

<?php
session_start();

include '../dbh.php';

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];

if (empty($fname)) {
    header("Location: ../parts/signup.php?error=empty");
    exit();
}
if (empty($lname)) {
    header("Location: ../parts/signup.php?error=empty");
    exit();
}
if (empty($uid)) {
    header("Location: ../parts/signup.php?error=empty");
    exit();
}
if (empty($pwd)) {
    header("Location: ../parts/signup.php?error=empty");
    exit();
} else {
$sql = "SELECT uid FROM users WHERE uid='$uid'";
$result = mysqli_query($conn, $sql);
$uidcheck = mysqli_num_rows($result);
if ($uidcheck > 0) {
    header("Location: ../parts/signup.php?error=username");
    exit();
} else {
    $enpwd = password_hash($pwd, PASSWORD_DEFAULT);
    $sql = "UPDATE users (fname, lname, uid, pwd) 
    VALUES ('$fname', '$lname', '$uid', '$enpwd')";

    $result = mysqli_query($conn, $sql);
    header("Location: ../index.php");}
}

----- 登录.inc.php:

<?php
session_start();
include '../dbh.php';

$uid = $_POST['uid'];
$pwd = $_POST['pwd'];

$sql = "SELECT * FROM users WHERE uid='$uid'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$hash_pwd = $row['pwd'];
$hash = password_verify($pwd, $hash_pwd);

if ($hash == 0 ) {
    header("Location: ../index.php?error=empty");
} else {
    $sql = "SELECT * FROM users WHERE uid='$uid' AND pwd='$hash_pwd'";
    $result = mysqli_query($conn, $sql);

    if (!$row = mysqli_fetch_assoc($result)) {
        echo "Your username or password is incorrect!";
    } else {
        $_SESSION['id'] = $row['id'];
    }
    header("Location: ../pages/dashboard.php");
}
?>

--------- 侧边栏:

    <?php
    session_start();
    if(!isset($_SESSION['id'])){ //if login in session is not set
        header("Location: ../index.php");
    }
    include '../include/idtouser.sinc.php';
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title>LoginSystem</title>
        <link rel="stylesheet" href="../css/global.css" type="text/css">
    </head>
    <body>
    <header id="closeNav">
        <nav>
              <div onclick="toggleNav()" id="star"><span class="hamburgerico">☰</span></div>
              <div class="profilePicture"></div>
              <div id="username">Username</div>
              <div id="rank"><p>Rank</p></div>

            <ul>
              <li><a href="#">Dashboard</a></li>
              <li><a href="#">Projects</a></li>
              <li><a href="#">Placeholder</a></li>
              <li><a href="#">Placeholder</a></li>
              <li><a href="#">Placeholder</a></li>
              <li><a href="../include/logout.inc.php">Log Out</a></li>
            </ul>
        </nav>
</header>

我这样做的正确方法是什么?

未回答的问题: 并且 -- 有没有办法可以将我的排名 (1,2,3,4,5) 转换为实际排名名称,然后再将它们发布到侧边栏中?

【问题讨论】:

  • 2016 年自制的注册脚本听起来不太对。
  • 您的脚本容易受到 sql 注入的攻击
  • @MasivuyeCokile 我知道,我还没有解决这个问题,不过我知道:D

标签: php mysql sidebar rank


【解决方案1】:

在登录页面中,添加:

$_SESSION['uid']        =   $row[1];

假设 row[1] 包含用户 ID 或用户名

然后在侧边栏中:

<?php echo $_SESSION['uid']; ?>

我不知道排名是干什么用的,但你可以用与用户名类似的方式呼出排名

【讨论】:

  • 嘿,不知道为什么,但这对我不起作用(?)我将 $row[1] 编辑为 2,因为这是我的 UID,但它仍然不起作用(? )
  • 我能否再问一个问题,如果排名为“1->5”,我如何在将其发布到侧边栏之前将其转换为实际排名? :D
  • 你好,@fizzi 我只是想知道你是否也能回答上面的问题:D
  • 如果您使用的是 phpmyadmin,则添加另一个名为 rank 的字段,其值为 1,2,3,4,5 作为枚举类型。假设它存储为 row[4]。然后以同样的方式在登录页面 $_SESSION['rank'] = $row[4];然后在侧边栏中:
猜你喜欢
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
相关资源
最近更新 更多