2015-02-07 71 views
-3

接下來是關於這個話題的最後一個問題。我試圖在我在我的網站上創建的帖子發表評論。事情是,評論沒有顯示出來。爲什麼我的評論沒有出現在這個PHP腳本中?

下面的代碼:

<?php 
         $getid = $_GET['id']; 

         // Get relevent comments 
         $get_comments = mysql_query("SELECT * FROM post_comments WHERE post_id='$getid' ORDER BY id DESC"); 
         $count = mysql_num_rows($get_comments); 
         if ($count != 0) { 
          while ($comment = mysql_fetch_assoc($get_comments)) { 

           $comment_body = $comment['post_body']; 
           $posted_to = $comment['posted_to']; 
           $posted_by = $comment['posted_by']; 
           $removed = $comment['post_removed']; 

           $comments = "<b><a href='$posted_by' target='_blank'>$posted_by</a> said: <br /></b>".$comment_body."<hr /><br />"; 
          } 
         } 
         else 
         { 
          // Do nothing! 
         } 
         echo " 
         <br /> 
         <div class='newsFeedPost'> 
         <div class='newsFeedPostOptions'> 
         <a href='javascript:;' onClick='javascript:toggle$id()'>Show Comments</a> 
         <div style='float: left;'> 
         <a href='$added_by'><img src='$profilepic_info' height='60' /></a> 
         </div> 
         <div class='posted_by'><a href='$added_by'>$added_by</a> wrote:</div> 
         <br /><br /> 
         <div style='max-width: 600px; height: auto;'> 
         $body<br /><br /><br /><p /> 
         </div> 
          Like &ndash; Re-Shout! &ndash; Comment 
         <br />$comments 
         </div> 
         </div> 
         "; 
         } 
} 
?> 

爲什麼它沒有顯示出來任何想法?另外,不要介意MySQL代碼,它僅用於測試目的!

我也聽說使用AJAX將是一個不錯的選擇。我會怎麼做? (我對PHP等頗爲陌生)

+0

你有厚厚的答案這個問題在這裏http://stackoverflow.com/questions/28379010/comments-on-posts-php – 2015-02-07 08:11:59

+0

看一看手冊在php.net/mysql_query - 此功能如果有錯誤返回'false'。因此,在通話後對其進行測試是一個好主意。 'if(!$ get_comments){die(mysql_error()); }'。 – halfer 2015-02-07 08:17:55

+0

「如何做AJAX」需要一些研究 - 看看「PHP AJAX示例PDO」,看看如何做到這一點。網絡上有成千上萬種。 ':-)' – halfer 2015-02-07 08:19:25

回答

0

試試吧。如果我理解你的代碼,我喜歡它。如果您需要更多,請給我們您的完整代碼。

<?php 
$getid = $_GET['id']; 

// Get relevent comments 
$get_comments = mysql_query("SELECT * FROM post_comments WHERE post_id='$getid' ORDER BY id DESC"); 
$count = mysql_num_rows($get_comments); 
if ($count > 0) { 
while ($comment = mysql_fetch_assoc($get_comments)) { 
$comment_body = $comment['post_body']; 
$posted_to = $comment['posted_to']; 
$posted_by = $comment['posted_by']; 
$removed = $comment['post_removed']; 

    } 
} 
else 
{ 
// Do nothing! 
} 
echo ' 
<br /> 
<div class="newsFeedPost"> 
    <div class="newsFeedPostOptions"> 
    <a href="javascript:;" onClick="javascript:toggle'.$id.'()">Show Comments</a> 
      <div style="float: left;"> 
      <a href="'.$added_by.'"><img src="'.$profilepic_info.'" height="60" /></a> 
      </div> 
       <div class="'.posted_by.'"><a href="'.$added_by.'">'.$added_by.'</a> wrote:</div> 
        <br /><br /> 
        <div style="max-width: 600px; height: auto;"> 
        '.$body.'<br /><br /><br /><p /> 
        </div> 
         Like &ndash; Re-Shout! &ndash; Comment 
        <br /><b><a href='.$posted_by.' target='_blank'>'.$posted_by.'</a> said: <br /></b>'.$comment_body.'<hr /><br /> 
     </div> 
</div> 
'; 
} 
} 
?> 
+0

不幸的是,沒有奏效。謝謝你給它一個嘗試! – 2015-02-07 13:48:00

相關問題