这是我的 actions.php 文件,它被发送到 ajax 文件以在 div 中显示结果
if(isset($_POST["action"]))
{
$UserID=getUserID();
if($_POST["action"] == "LoadData") {
$a=1;
$statement = $conn->prepare("SELECT sellers.room,users.username,payments.status,
payments.action
FROM payments INNER JOIN sellers
ON payments.seller_id=sellers.seller_id INNER JOIN users
ON payments.user_id=users.user_id AND payments.status=1
AND payments.action=0 AND payments.user_id= ?");
$statement->execute([$UserID]);
$result = $statement->fetchAll();
if($result) {
foreach($result as $row) {
//RESULT HERE
}
}
}
getUserID() 来自这个文件,我已经将它链接到 actions.php 文件
function getUserID(){
global $conn;
$sql = "SELECT user_id FROM users WHERE username=:user";
$db=$conn->prepare($sql);
$db->bindParam(':user',$_SESSION['user']);
$db->execute();
$db->setFetchMode(PDO::FETCH_ASSOC); //PDO::FETCH_OBJ
$result = $db->fetchAll();
$UserID = "Error, try again";
foreach($result as $row){
$UserID = htmlspecialchars($row['user_id']);
}
return $UserID;
}
AJAX 文件在这里
$(document).ready(function(){
fetchData();
function fetchData()
{
var action = "LoadData";
$.ajax({
url : "actions.php",
method:"POST",
data:{action:action},
success:function(data){
$('#result').html(data); //the div id is #result and thats where this data is to be displayed
}
});
}
});
当我执行该查询时,没有显示任何结果但我在数据库中有数据。在控制台中没有错误,它只是说 actions.php 文件加载成功。但没有结果。请有人帮我指出我这段代码中的一些错误。 并请帮忙解释