0
我在登錄後顯示用戶在註冊時輸入的信息時遇到問題。一旦我註冊,信息將正確顯示在我的帳戶頁面上,但是當我註銷並重新登錄時,信息會消失。我如何訪問我的用戶登錄時的信息?使用MySQL和PHP顯示用戶信息
這是我的用戶註冊。
<?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!";
}
?>
真誠地懷疑你的用戶註冊代碼工作。不要在查詢中使用缺少的引號'WHERE用戶名= $用戶名和密碼= $密碼「)' –
,這並不會幫助你'error_reporting(0);' –
[你的腳本存在SQL注入攻擊風險。 (http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –