2016-04-05 43 views
0

在我一直在努力的当前页面上,我已经设置了代码,它可以用作实时博客/更新类型的系统。问题是,我从我的数据库中加载我的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 "<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>

第二页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 "<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 { 
 
}

+0

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

回答

0

问题是,您正在将新数据加载到类center-match-container的任何HTML元素中,但是您使用AJAX获得的新数据还包含类center-match-container的元素,因此第二次调用将它加载到两个位置,等等。

matchinforequest.php删除<div class='center-match-container'>和相应的</div>它应该工作。作为一个方面说明,您并未使用预处理语句,而是直接将$ _GET ['id']的内容放入数据库查询中。这意味着有人可以通过将id设置为0'; DELETE FROM matchinfo; '之类的东西来轻松擦除数据库。请查看准备好的声明http://php.net/manual/en/mysqli.prepare.php并更新您的代码以避免此安全风险!

+0

嗯....我刚刚删除了'中心匹配容器',它仍然复制它,当它得到新的数据。我也尝试删除'.match-container',并且之后也重复。我现在也会检查一下准备好的语句,并确保我的代码是安全的。 – BenJ30

+0

数据库中是否有多个条目?尝试在'matchinforequest.php'中添加一个计数器('$ count = 0;'在'SELECT'之前,然后在$ while之后并且在echo $ postheader' echo之前'count count';行计数是$ count ')来检查。 –

+0

第一部分去的地方,'$ count = 0;'在语音标记/ mysqli语句中不起作用。那么如果它在语音标记之前就会抛出一个语法错误。 – BenJ30

相关问题