向登录用户显示数据-PHP



我正在创建一个显示登录历史的页面。我的代码当前显示所有日志,并且应该只显示登录用户的日志历史记录。

数据库->日志:

log_id | user_email | ip_address | time 
-------+------------+------------+-----
1    | ml@a.com   | 123.13.13  | 1:30 

LogHistory.php页面:

<?php
$stmt = $dbh->prepare("SELECT * FROM Logs ORDER BY log_id ASC");
$stmt->execute();
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>

我试过这个代码:

<?php
$LoggedInUser = $_SESSION['user'];
$stmt = $dbh->prepare("SELECT * FROM Logs WHERE user_email = $LoggedInUser ORDER BY log_id ASC");
$stmt->execute();
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>

使用上面的代码,我得到了这个错误消息:

PHP Fatal error:  Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':user@example.com ORDER BY log_id ASC'

您只需要在准备好的语句中使用参数

<?php
$LoggedInUser = $_SESSION['user'];
$stmt = $dbh->prepare("SELECT * FROM Logs WHERE user_email = ? ORDER BY log_id ASC");
$stmt->execute([$LoggedInUser]);
if ($stmt->rowCount() == 0) {
echo 'Log history are empty.';
} else {
// Data we collected from the registered user
}
?>

这里的实时PHP代码

最新更新