当用户使用用户名登录时获取用户ID&密码 [英] Obtain User ID when a user logs in using Username & Password
问题描述
我需要输入USER表的USER_ID字段作为另一个表中的外键。
因此,我认为我需要在用户登录时启动一个会话。我很难让它在任何地方回显。我可以开始一个会话,允许我回显用户名,但不能复制它的ID。
有什么协助吗?我的处理代码如下。
处理脚本:
我也粘贴了暂时试图回显的部分进行测试。
<?php
session_start();
$ error_message = array();
$ error = false;
$ dbhost =localhost;
$ dbname =xxx;
$ dbuser =xxx;
$ dbpass =xxx;
$ conn = new PDO(mysql:host = $ dbhost; dbname = $ dbname,$ dbuser,$ dbpass);
$ user = $ _POST ['uname'];
$ password = $ _POST ['pword'];
if($ user ==''){
$ error_message [] ='您尚未输入所需的用户名。
$ error = true;
if($ password ==''){
$ error_message [] ='您没有输入所需的用户名';
$ error = true;
$ result = $ conn-> prepare(SELECT * FROM USER WHERE USERNAME =:un and PASSWORD =:pw);
$ result-> bindParam(':un',$ user);
$ result-> bindParam(':pw',$ password);
$ result-> execute(); ($ rows = $ result-> fetch(PDO :: FETCH_NUM)){
$ b if($ rows> 0 ){
$ _SESSION ['Logged_In'] = true;
$ _SESSION ['username'] = $ user;
$ _SESSION ['USER_ID'] = $ rows ['USER_ID'];
header('Location:index.php');
}
else {
$ error_message [] ='您的用户名和密码不正确。再试一次。';
$ error = true;
}
if($ error){
$ _SESSION ['ERROR_MESSAGE'] = $ error_message;
session_write_close();
header(location:log_in.php);
exit();
}}
?>
我想要显示USER_ID:
<?php
session_start();
if(isset($ _ SESSION ['Logged_In']))
{
echo'< br>< br>';
echo'您已登录为';
echo $ _SESSION ['username'];
echo'< br>';
echoID =。$ _ SESSION ['USER_ID'];
echo'< br>';
echo'< a href =logout.php>
点击此处注销。< / a>';
}
else
{
echo'< br>';
echo'您没有登录!< br>';
echo'< a href =log_in.php>点击此处登录,< / a>< br>';
echo'< a href =register.php>或点击此处注册。< / a>';
}
?>
使用FETCH_ASSOC而不是FETCH_NUM
I need to input the USER_ID field of my USER table as a foreign key in another table. Therefore, I think I need to start a session when the user logs in. I'm having difficulty getting it to echo anywhere. I am able to start a session that allows me to echo the USERNAME, but cannot replicate it for the ID.
Any assistance please? My processing code is below. I have also pasted the section where I am temporarily trying to echo it for testing.
Processing script:
<?php
session_start();
$error_message = array();
$error = false;
$dbhost = "localhost";
$dbname = "xxx";
$dbuser = "xxx";
$dbpass = "xxx";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$user = $_POST['uname'];
$password = $_POST['pword'];
if($user == '') {
$error_message[] = 'You have not entered the required username.';
$error = true;
}
if($password == '') {
$error_message[] = 'You have not entered the required username';
$error = true;
}
$result = $conn->prepare("SELECT * FROM USER WHERE USERNAME= :un AND PASSWORD= :pw");
$result->bindParam(':un', $user);
$result->bindParam(':pw', $password);
$result->execute();
while ($rows = $result->fetch(PDO::FETCH_NUM)) {
if($rows > 0) {
$_SESSION['Logged_In'] = true;
$_SESSION['username'] = $user;
$_SESSION['USER_ID'] = $rows['USER_ID'];
header('Location: index.php');
}
else{
$error_message[] = 'Your username and password are not correct. Try again.';
$error = true;
}
if($error) {
$_SESSION['ERROR_MESSAGE'] = $error_message;
session_write_close();
header("location: log_in.php");
exit();
}}
?>
Where I want the USER_ID to appear:
<?php
session_start();
if(isset($_SESSION['Logged_In']))
{
echo '<br><br>';
echo 'You are logged in as ';
echo $_SESSION['username'];
echo '<br>';
echo "ID = ".$_SESSION['USER_ID'];
echo '<br>';
echo '<a href="logout.php">
Click here to log out.</a>';
}
else
{
echo '<br>';
echo 'You are not logged in!<br>';
echo '<a href="log_in.php">Click here to log in,</a><br>';
echo '<a href="register.php">or click here to register.</a>';
}
?>
use FETCH_ASSOC instead of FETCH_NUM
这篇关于当用户使用用户名登录时获取用户ID&密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!