2016-04-05 42 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

相關問題