AJAX刷新 - 复制已从数据库加载的数据

时间:2016-04-05 19:18:29

标签: php ajax

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

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

更多Clear Bulletpoint表格

  • PHP查询数据库,回显数据。
  • AJAX每隔15000毫秒检查另一个php页面并回显第一页。
  • 它不仅仅发布新内容,而只是复制原始内容。 (可以有双重职位甚至是三重职位。似乎有所不同)

我只是真正进入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 "<div class='center-match-container'>
            <div class='match-information'>
                <div class='post-container'>
                    <div class='post-left'>
                        <img class='post-type-icon' src='images/icons/$type' />
                    </div>
                    <div class='post-right'>
                        <h3 class='header-top'>$postheader</h3>
                        <span class='time-red-right'>$posttime</span>
                        <br />
                            <br />
                        <p class='post-content'>$postcontent</p>
                    </div>
                </div>
            </div>
          </div>";        
    }
    
    $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 '<div class="match-notice"><h4>There are currently no updates, this page will auto-update when there are new updates.</h4></div>';
 }
 
 echo $id;
 ?>
 <script>
     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);
 </script>
&#13;
&#13;
&#13;

第二页matchinforequest.php?id =(同样这个id是6)

&#13;
&#13;
$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 "<div class='center-match-container'>
            <div class='match-information'>
                <div class='post-container'>
                    <div class='post-left'>
                        <img class='post-type-icon' src='images/icons/$type' />
                    </div>
                    <div class='post-right'>
                        <h3 class='header-top'>$postheader</h3>
                        <span class='time-red-right'>$posttime</span>
                        <br />
                            <br />
                        <p class='post-content'>$postcontent</p>
                    </div>
                </div>
            </div>
          </div>";
    }

    $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 {
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

问题是你要将新数据加载到类center-match-container的任何HTML元素中,但是你用AJAX获得的新数据也包含一个类center-match-container的元素,所以第二个call将它加载到两个地方,依此类推。

matchinforequest.php移除<div class='center-match-container'>和相应的</div>,它应该有效。

作为旁注,您没有使用预准备语句,而是将$ _GET [&#39; id&#39;]的内容直接放入数据库查询中。这意味着有人可以通过将id设置为0'; DELETE FROM matchinfo; '来轻松擦除数据库。请查看准备好的语句http://php.net/manual/en/mysqli.prepare.php并更新您的代码以避免此安全风险!