简体   繁体   中英

Display Users Information with MySQL and PHP

I am having problems displaying my users information that they inputted at sign up once they have logged in again. Once I sign up the information will be displayed properly on my account page but when I log out and log back in the information disappears. How can I access the information for when my users login?

This is my user sign up.

<?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(); 
}
?>

This is my user login.

<?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) $$key = stripslashes($val); Double $$

2) Data which you are putting into $_SESSION is empty;

3) session_start(); 2 times in one program's space

4) And everything else that the guys have said above

        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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM