使用MySQL和PHP显示用户信息

时间:2015-12-11 18:02:11

标签: php mysql

我在显示用户信息时遇到问题,这些信息是他们再次登录后在注册时输入的。一旦我注册,信息将在我的帐户页面上正确显示,但是当我退出并重新登录时,信息会消失。如何访问用户登录时的信息?

这是我的用户注册。

<?php

include 'global_settings.php';

function NewUser() { 
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];  
    $email = $_POST['email']; 
    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = "INSERT INTO userlogin (firstName, lastName, email, username, password) VALUES ('$firstName', '$lastName', '$email', '$username', '$password')"; 
    $data = mysql_query ($query)or die(mysql_error()); 
    if($data) { 
        session_start();
        $_SESSION["firstName"] = $firstName;
        $_SESSION["lastName"] = $lastName;
        $_SESSION["userName"] = $username;
        $_SESSION["email"] = $email;
        header("Location: ../chooseyoursport.php");
    } 
}
NewUser();

function SignUp() { 
    if(!empty($_POST['username'])){ //checking the 'user' name which is from Sign-Up.html, is it empty or have some text 
        $query = mysql_query("SELECT * FROM userlogin WHERE Username = $username AND Password = $password") or die(mysql_error()); 
        if(!$row = mysql_fetch_array($query) or die(mysql_error())) { 
            newuser(); 
        } else { 
            echo "SORRY...YOU ARE ALREADY REGISTERED USER..."; 
        } 
    } 
} 
if(isset($_POST['submit'])) { 
    SignUp(); 
}
?>

这是我的用户登录信息。

<?php
    error_reporting(0);
    session_start();
    include 'global_settings.php';

    //Convert POST to normal variables
    $password = $_POST['password'];
    $username = $_POST['username'];

    $sql = mysql_query("SELECT * FROM userlogin WHERE Username='$username' AND Password='$password'");
    $login_check = mysql_num_rows($sql);

    // if login_check is greater than 0 then it will register a session (meaning if the user exists username and password are both correct)

    if($login_check > 0){
        while($row = mysql_fetch_array($sql)){
            foreach( $row AS $key => $val){
                $$key = stripslashes($val);
            }
            session_start();
            $_SESSION["firstName"] = $firstName;
            $_SESSION["lastName"] = $lastName;
            $_SESSION["userName"] = $username;
            $_SESSION["email"] = $email;
            header("Location: ../chooseyoursport.php");
            //echo "It worked";
        }
    } else {
        echo "You could not be logged in! Either your username or password is incorrect <br> Please try again!";
    }


?>

1 个答案:

答案 0 :(得分:0)

1)$$key = stripslashes($val); Double $$

2)您放入$ _SESSION的数据为空;

3)session_start();在一个程序的空间中2次

4)以及其他人说过的其他事情

        foreach( $row AS $key => $val){
            $$key = stripslashes($val);
        }
        session_start();
        $_SESSION["firstName"] = $firstName;
        $_SESSION["lastName"] = $lastName;
        $_SESSION["userName"] = $username;
        $_SESSION["email"] = $email;
        header("Location: ../chooseyoursport.php");