我已经将记录的用户名存储在 $_SESSION[user_name] 变量中。如何另外将 user_id 存储在 $_SESSION[user_id] 变量中。我尝试使用 $stmt->fetch_array(MYSQLI_NUM); 但没有运气。
我有一个这样的 MySQL 表:
登录:
+---------------+-------------+-------------+-------------+
| auto_id | username | password | user_id |
+---------------+-------------+-------------+-------------+
| 0 | a | b | 1 |
+---------------+-------------+-------------+-------------+
| 1 | c | d | 2 |
+---------------+-------------+-------------+-------------+
| 2 | e | f | 3 |
+---------------+-------------+-------------+-------------+
我的 php 登录验证器是:
<?php
session_start();
require_once('php/config.php');
if(isset($_POST['submit'])){
$username = $_POST['user_name'];
$password = $_POST['pwd'];
$user_id_col = "3";
$stmt = $con->prepare("SELECT * FROM tapp_login WHERE username = ? AND password = ? LIMIT 1");
$stmt->bind_param('ss', $username, $password);
$stmt->execute();
$stmt->bind_result($username, $password);
$stmt->store_result();
if($stmt->num_rows == 1) //To check if the row exists
{
while($stmt->fetch())
{
$_SESSION['LOGIN_STATUS']=true;
$_SESSION['user_name'] = $username;
$_SESSION['user_id'] = $user_id;
echo 'true';
}
}
else {
echo 'false';
}
$stmt->close();
}
else{
}
$con->close();
?>