你好我有一个大问题..我正在创建一个用户登录网站,但问题是,如果我在数据库中有2个或更多用户,如果我尝试使用我的第二个用户的凭据登录它只给了我第一个用户信息。任何帮助都会很感激。
的login.php
include '../init/init.php';
if (empty($_POST) === false) {
// ...
$username = $_POST['log_username'];
$password = $_POST['log_password'];
$login = login($username, $password);
if ($login === false){
die(msg1(0, 'Password is incorrect.'));
}else{
$_SESSION['id'] = $login;
die(msg1(1,"members.php"));
exit();
}
}
users.php(我这里有我所有的用户功能)
<?php
// ...
function user_id_from_username($username) {
$username = sanitize($username);
$query_user_id = mysql_query("SELECT (`id`) FROM `users` WHERE `username` = '$username'");
return mysql_result($query_user_id, 0, 'id');
}
function login($username, $password) {
$id = user_id_from_username($username);
$username = sanitize($username);
$password = md5($password);
$query_login = mysql_query("SELECT COUNT(`id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'");
return (mysql_result($query_login, 0) == 1) ? $id : false;
}
?>
这会在登录后显示个人资料:
// ...
if (logged_in() === true){
$session_user_id = $_SESSION['id'];
$sql = mysql_query("SELECT * FROM users");
$id = 'id';
$username = 'username';
$rows = mysql_fetch_assoc($sql);
}
$errors = array();
// show profile based on $rows
答案 0 :(得分:1)
从评论部分的代码判断,我建议将代码更改为:
$session_user_id = $_SESSION['id'];
$sql = mysql_query("SELECT * FROM users WHERE `id`='$session_user_id'");
我将假设您事先清理过$_SESSION['id']
。