如何使用登录页面中的会话变量显示用户数据?

时间:2018-08-04 13:12:03

标签: php session mysqli passwords

我需要有关使用Session变量的帮助,我想在下一个屏幕上显示“登录用户”的数据。 这是 Login.php

    <?php 
    session_start();
        $link = mysqli_connect("shareddb-h.hosting.stackcp.net", 
"StudentDetails-33314e4f", "2kg3dvardk", "StudentDetails-33314e4f");

        if (mysqli_connect_error()) {

            die ("There was an error connecting to the database");

        } 
        $username = "";
        $LoginEmail = (isset($_POST['LoginEmail']) && 
!empty($_POST['LoginEmail'])) ? $_POST['LoginEmail'] : false;
        $LoginPassword = (isset($_POST['LoginPassword']) && 
!empty($_POST['LoginPassword']))? $_POST['LoginPassword']: false;


        $query ="SELECT `Email`,`ID`, `Password` FROM `StudentInfo` WHERE 
Email='$LoginEmail' AND Password='$LoginPassword'";
        $result = mysqli_query($link, $query);    
        $count = mysqli_num_rows($result);
        if($count == 1 )
        {
            $query ="SELECT `ID` FROM `StudentInfo` WHERE Email='$LoginEmail' 
AND Password='$LoginPassword'";
            $result = mysqli_query($link, $query);
            $array = array();
            while($row = mysqli_fetch_assoc($result))
            {
                $array[] = $row;
            }
            $_SESSION['userid'] =$array ;
            header("location: StudentInformation.php");
        }
        else {

            $_SESSION['errMsg'] = "Invalid username or password";

        }


    ?>

这是我要在其上显示数据的 STudentInfo 页面。

    <?php 
    session_start();
        $link = mysqli_connect("shareddb-h.hosting.stackcp.net", 
"StudentDetails-33314e4f", "2kg3dvardk", "StudentDetails-33314e4f");

        if (mysqli_connect_error()) {

            die ("There was an error connecting to the database");

        }
        $query = "SELECT `FirstName` FROM `StudentInfo` WHERE ID = 
'".$_SESSION['userid[ID]']."'";
        $FN = mysqli_query($link,$query);
        $FirstName = mysqli_fetch_assoc($FN);
        print_r($FirstName);
?>

我尝试了一切,似乎mysqli_fetch_assoc在Login.php和StudentInfo页面中不起作用。请帮我解决这个问题,如果您有其他选择,欢迎您!

2 个答案:

答案 0 :(得分:0)

在您的第一个代码中,您必须像这样更改代码:

  <?php 
    session_start();
        $link = mysqli_connect("localhost", "user", "pass", "db"); //take care, you should avoid posting sensitive data 

        if (mysqli_connect_error()) {

            die ("There was an error connecting to the database");

        } 
        $username = "";
        $LoginEmail = (isset($_POST['LoginEmail']) && 
!empty($_POST['LoginEmail'])) ? $_POST['LoginEmail'] : false;
        $LoginPassword = (isset($_POST['LoginPassword']) && 
!empty($_POST['LoginPassword']))? $_POST['LoginPassword']: false;


        $query ="SELECT `Email`,`ID`, `Password` FROM `StudentInfo` WHERE 
Email='".$LoginEmail."' AND Password='".$LoginPassword."'"; 
        $result = mysqli_query($link, $query);    
        $count = mysqli_num_rows($result);
        if($count == 1 )
        {
            $query ="SELECT `ID` FROM `StudentInfo` WHERE Email='".$LoginEmail."' 
AND Password='".$LoginPassword."'";
            $result = mysqli_query($link, $query);
            $array = array();
            while($row = mysqli_fetch_assoc($result))
            {
                $array[] = $row;
            }
            $_SESSION['userid'] =$array['userid'] ; //instead of saving the complete row, you can save just the variable
            header("location: StudentInformation.php");
        }
        else {

            $_SESSION['errMsg'] = "Invalid username or password";

        }


    ?>

然后在第二个代码中,您只需要替换查询

 $query = "SELECT `FirstName` FROM `StudentInfo` WHERE ID = '".$_SESSION['userid']."'"; 

希望这会有所帮助

答案 1 :(得分:0)

首先,谢谢大家的考虑。我找到了问题的答案,并希望发布所需的更改。 我只是稍微修改了上面的代码,使其变得简单易用

if($count == 1 )
    {
        $query ="SELECT `ID` FROM `StudentInfo` WHERE Email='$LoginEmail' 
AND Password='$LoginPassword'";
        $result = mysqli_query($link, $query);

        $row = mysqli_fetch_assoc($result);

        $_SESSION['userid'] =$row['userid'] ;
        header("location: StudentInformation.php");
    }

在代码的第二部分:print_r($ FirstName ['FirstName']);