doushi1510 2015-12-11 18:02
浏览 85

使用MySQL和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条回答 默认 最新

  • dongzhuan1185 2015-12-11 18:15
    关注

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)