ajax 出现重复数据,AJAX刷新 - 重复已经加载的数据来自数据库的数据

博主遇到一个技术问题,其网页使用PHP和AJAX实现实时更新功能,但导致已加载的内容每15000毫秒被复制,而不是仅显示新内容。问题在于AJAX请求不断重复加载已存在的数据库数据。解决方案可能涉及调整AJAX请求以仅在有新内容时更新页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我一直在努力的当前页面上,我已经设置了代码,它可以用作实时博客/更新类型的系统。问题是,我从我的数据库中加载我的PHP中的东西,然后我有AJAX链接到另一个文件,它将获取数据库内容并刷新它包含在我的网站上的区域。AJAX刷新 - 重复已经加载的数据来自数据库的数据

因此'这意味着它会自动更新每15000毫秒与来自数据库的数据。问题是,它已经加载了现有的数据。所以无论如何。每15000毫秒将刷新该div,因此页面上已有的数据将被复制。

更加清晰Bulletpoint形式

PHP查询数据库,回声的出来的数据。

AJAX每15000毫秒检查一个php页面,并在第一页上显示回显。

不只是发布新内容,它只是复制原始内容。 (可以有双帖或甚至tripple。它似乎有所不同)

我只是真的进入PHP,我没有把很多时间放进去,我的AJAX知识是不存在的,所以它提出这样的问题。我试着搜索如何仅在第一页上回显现有数据,即使第二页正在处理更新。

但是,这是代码,对不起,如果它是凌乱的,或正确地做的事情。我仍然在学习这种语言。

第一页matchdayupdates.php?ID =(在这种情况下,id为6)

$id = $_GET['id'];

if(isset($_GET['id'])) {

$requestMatchInformation = mysqli_query($connect, "SELECT * FROM matchinfo WHERE pageid='$id' LIMIT 500");

while ($row = mysqli_fetch_assoc($requestMatchInformation)) {

$pageid = $row['pageid'];

$type = $row['type'];

$postheader = $row['postheader'];

$postcontent = $row['postcontent'];

$posttime = $row['posttime'];

echo "

%24type

$postheader

$posttime

$postcontent

";

}

$requestEventsInformation = mysqli_query($connect, "SELECT * FROM events WHERE id='$id'");

while($row = mysqli_fetch_assoc($requestEventsInformation)) {

$opponent = $row['opponent'];

$datetime = $row['datetime'];

$datetimedisplay = $row['datetimedisplay'];

$location = $row['location'];

$datepassed = $row['datepassed'];

$rowonescore = $row['rowonescore'];

$rowtwoscore = $row['rowtwoscore'];

$rowoneplayers = $row['rowoneplayers'];

$rowtwoplayers = $row['rowtwoplayers'];

}

}

else {

}

if(!$requestEventsInformation && !$requestMatchInformation) {

echo '

There are currently no updates, this page will auto-update when there are new updates.
';

}

echo $id;

?>

var auto_refresh = setInterval(function() {

$('.center-match-container').fadeOut('slow', function() {

$(this).load('/esports/match/matchinforequest.php?id=<?php echo $id; ?>', function() {

$(this).fadeIn('slow');

});

});

$.ajaxSetup({ cache: true });

}, 15000);

第二页matchinforequest.php?ID =(再次此ID 6)

$id = $_GET['id'];

if(isset($_GET['id'])) {

$requestMatchInformation = mysqli_query($connect, "SELECT * FROM matchinfo WHERE pageid='$id' LIMIT 500");

while ($row = mysqli_fetch_assoc($requestMatchInformation)) {

$pageid = $row['pageid'];

$type = $row['type'];

$postheader = $row['postheader'];

$postcontent = $row['postcontent'];

$posttime = $row['posttime'];

echo "

%24type

$postheader

$posttime

$postcontent

";

}

$requestEventsInformation = mysqli_query($connect, "SELECT * FROM events WHERE id='$id'");

while($row = mysqli_fetch_assoc($requestEventsInformation)) {

$opponent = $row['opponent'];

$datetime = $row['datetime'];

$datetimedisplay = $row['datetimedisplay'];

$location = $row['location'];

$datepassed = $row['datepassed'];

$rowonescore = $row['rowonescore'];

$rowtwoscore = $row['rowtwoscore'];

$rowoneplayers = $row['rowoneplayers'];

$rowtwoplayers = $row['rowtwoplayers'];

}

echo "Received Data";

}

else {

}

2016-04-05

BenJ30

+0

而不是追加数据,取而代之的是什么问题... –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值