-1
我需要輸入USER表的USER_ID字段作爲另一個表中的外鍵。 因此,我認爲我需要在用戶登錄時啓動一個會話。我很難讓它在任何地方回顯。我可以開始一個會話,允許我回顯USERNAME,但不能複製該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[] = '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();
}}
?>
我想要的USER_ID出現:
<?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>';
}
?>
什麼是會議的問題呢? –
您是否嘗試過使用FETCH_ASSOC而不是FETCH_NUM? –
@LucaIaco完美無缺!非常感謝你:) :) – user3183576