您好所有 PHP 大师...我希望我不会打扰任何人...如果我的问题是错误的或不适合本节,我很抱歉。我的英语不是那么好。我试图解决这个问题几天并尝试寻找答案,但找不到任何可以解决我的问题的解决方案。首先,我道歉,因为老实说,我是 PHP 的新手,我真的是一个新手,需要你的指导。
我已经有一个登录页面(login.php)。当用户登录时,他们将被引导到用户页面(useracc-test.php)。它假设根据他们的用户名和密码会话显示他们的个人数据,但它显示错误(注意:未定义索引:C:\xampp 中的用户名\htdocs\eventsite\useracc-test.php 在第 42 行
警告:mysql_fetch_array() 期望参数 1 是资源,对象在 C:\xampp\htdocs\eventsite\useracc-test.php 的第 51 行 Hello, ().) 中给出。
我在数据库连接问题上没有问题,因为我可以在“useracc-test.php”中显示所有用户数据。唯一的问题是,我无法根据用户的登录会话显示 SELECTED 数据。我希望用户检索自己的数据而不是每个人的数据。
下面是用户页面“useracc-test.php”。这是用户登录成功后的页面,假设显示自己的个人数据。需要你的帮助.. Tq 再次.. Linda May。
<?php
//useracc-test.php
/**
* Start the session.
*/
session_start();
/**
* Include our MySQL connection.
*/
// require 'lib/password.php';
require 'connect-test.php';
/*
THIS ONE WORKS FINE..BUT IT DISPLAY ALL ROWS...BUT THE ONE BELOW "$_SESSION['user_id']"
DISPLAYS ERROR
$sql = "SELECT name, telno, username FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "name: " . $row["name"]. " - telno: " . $row["telno"]. " username" . $row["username"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
*/
//THIS ONE BELOW DOES NOT WORK FINE..DISPLAY 2 ERRORS
/*Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in C:\xampp\htdocs\eventsite\useracc-test.php on line 49
Hello, (). */
//Notice: Undefined index: username in C:\xampp\htdocs\eventsite\useracc-test.php on line 42
//$sql = "SELECT name, username FROM users WHERE id = " . $_SESSION['user_id'] . ;
$sql = "SELECT name, username FROM users WHERE username = '" . $_SESSION['username'] . "'";
$result = $conn->query($sql);
//$result = mysql_query($sql);
if (!$result) { // add this check.
die('Invalid query: ' . mysql_error());
}
else{
$row = mysql_fetch_array($result);
echo "Hello, " . $row['name'] . " (" . $row['username'] . ").";
}
?>
登录.php
<?php
//login.php
/**
* Start the session.
*/
session_start();
/**
* Include password_compat library.
*/
require 'lib/password.php';
/**
* Include our MySQL connection.
*/
require 'connect.php';
//define variables and define to null.
$usernameError = $passwordError = "";
//If the POST var "login" exists (our submit button), then we can
//assume that the user has submitted the login form.
if(isset($_POST['login'])){
//Retrieve the field values from our login form.
$username = !empty($_POST['username']) ? trim($_POST['username']) : null;
$passwordAttempt = !empty($_POST['password']) ? trim($_POST['password']) : null;
//Retrieve the user account information for the given username.
$sql = "SELECT id, username, password FROM users WHERE username = :username";
$stmt = $pdo->prepare($sql);
//Bind value.
$stmt->bindValue(':username', $username);
//Execute.
$stmt->execute();
//Fetch row.
$user = $stmt->fetch(PDO::FETCH_ASSOC);
//If $row is FALSE.
if($user === false){
//Could not find a user with that username!
//PS: You might want to handle this error in a more user-friendly manner!
$usernameError = "Invalid username. Username is required";
$passwordError = "Invalid password. Password is required";
} else{
//User account found. Check to see if the given password matches the
//password hash that we stored in our users table.
//Compare the passwords.
$validPassword = password_verify($passwordAttempt, $user['password']);
//If $validPassword is TRUE, the login has been successful.
if($validPassword){
//Provide the user with a login session.
$_SESSION['user_id'] = $user['id'];
$_SESSION['logged_in'] = time();
//Redirect to our protected page, which we called useracc.php
header('Location: useracc-test.php');
exit;
} else{
//$validPassword was FALSE. Passwords do not match.
$passwordError = "Invalid password. Password is required";
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
<style>
.error {
color: #CB1313;
font-family:arial;
font-size: 10pt; }
</style>
</head>
<body>
<h1>Login</h1>
<form action="login.php" method="post">
<label for="username">Username</label>
<input class="input" name="username" maxlength="50" input type="text" type="text" value="" autocomplete="off" > <span class="error">* <?php echo $usernameError; ?></span> <br>
<label for="password">Password</label>
<input class="input" name="password" maxlength="16" input type="password" type="text" value="" autocomplete="off" > <span class="error">* <?php echo $passwordError; ?></span><br>
<input type="submit" name="login" value="Login">
</form>
</body>