从MYSQL数据库中检索用户名

时间:2016-12-19 10:41:31

标签: php mysql sidebar username rank

回答问题:

  

所以我已经为我的网站制作了登录和注册功能,但现在我愿意   喜欢将用户名和排名添加到侧边栏..但我不能   弄清楚如何做到这一点,这就是我向你寻求帮助的原因。   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");}
}
     

----- Login.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>
           

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

未解决的问题: AND - 有没有办法将我的排名(1,2,3,4,5)翻译成实际的排名,然后再将其发布到侧栏?

1 个答案:

答案 0 :(得分:1)

在登录页面中,添加:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="nmct.jaspernielsmichielhein.watchfriends.view.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/frame_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            <FrameLayout
                android:id="@+id/fragment_frame"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            </FrameLayout>

        </android.support.v4.widget.NestedScrollView>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>

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

然后在侧栏:

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

我不知道排名是什么,但你可以用与用户名

类似的方式回应排名