2012-01-04 52 views
1

試圖解決一個錯誤!我有一個阿賈克斯動力的社交網絡(與喜歡和評論等)。我有一個工作評論腳本,但是當我測試它時,我得到了原始帖子和評論的重複。每次輸出數據時,都會添加註釋並複製帖子和註釋,而不是僅添加評論。用ajax驅動的社交網絡複製mysql評論條目?

我已經使用jQuery的Ajax工作正常和PHP,可能是顯而易見的,因爲我新的jQuery和PHP我希望得到一些幫助?對不起,我的代碼armount如果需要

感謝 jQuery的

 $(".editable").live('click', function(){ 


     $(this).empty(); 

     $(this).mouseout(function() { 

      var comment = $(this).html(); 
      var postid = $(this).attr("pid"); 
      var commentuserid = $("#loggedin").attr("uid"); 


      if(comment == ""){ 

       return false; 


      }else{ 


        //alert(comment); 
        //alert(postid); 
        //alert(commentuserid); 


        var datastring = 'comment=' + comment + '&postid=' + postid + '&commentuserid=' + commentuserid; 


        //save with ajax clear box and then load back in 
        $.ajax({ 
        type: "POST", 
        url: "uploadcomment.php", 
        data: datastring, 
        success: function(data){ 


        $(".usermsg").html(data); 




        } 

        }); 


      } 
}); 



}); 

PHP給人當評論上傳

 <? 

    $sql = "SELECT post.post, post.posttime, post.pid_imageurl, post.likes, user.name, 
    comments.comment, user.uid_imageurl, comments.comment_uid, post.pid 
       FROM post 
       INNER JOIN user 
       ON post.uid=user.uid 
       LEFT JOIN comments 
       ON comments.comment_pid=post.pid 
       ORDER BY pid DESC"; 

    $result = mysql_query($sql); 

    while($row=mysql_fetch_assoc($result)){?> 

    <? 
    $pid = $row['pid']; 
    $formattime = date("g:i:a",$row['posttime']); 
    echo '<p class="speechbubble">'.$row['post'].'</p>'; 
    echo '<p class="msgholder" >Posted by '.$row['name'].' at '.$formattime. '<img class= "userprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/></p>'; 
?> 



    <div class="editable" pid="<? echo $row['pid'];?>" contentEditable="true"> 
      add comment... 
    </div> 

    <? 

    $sql_comments = "SELECT comments.comment, comments.comment_time, user.name, user.uid_imageurl FROM comments 
        INNER JOIN user 
        ON comments.comment_uid = user.uid 
        WHERE comment_pid = '$pid' ORDER BY cid DESC"; 


    $result_comments = mysql_query($sql_comments);     

     $numrows_comments=mysql_num_rows($result_comments);//num of rows 


       if($numrows_comments==0) //no comments 
      { 
       echo '<p>Comments</p><hr><br>'; 
       echo 'This post has no comments yet, be the first!'; 
       echo '<br><br><br><hr><br><br>'; 

      }else{ 
        echo '<p>Comments</p><hr><br>'; 
        while($row=mysql_fetch_assoc($result_comments)){ 
        $formattime_comment = date("g:i:a",$row['comment_time']); 
        echo '<p class="comment">'.$row['comment'].'</p>'; 
        echo '<p class="commentposter">posted by '.$row['name']. ' at ' .$formattime_comment. '<img class="commentprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/></p>'; 

      }//$sql_comments 

      echo '<hr><br><br>'; 

     }//else end 

    }//$sql 


    ?> 




    </div> 









</div> 
雙崗我可以減少 - >
+0

你沒有顯示你的更新代碼或處理HTML更新的JS。另外,請用正確的<?php打開php,因爲它只是<?是模棱兩可的,可以解釋爲xml – 2012-01-04 19:45:20

+0

更新代碼工作正常我很確定它的這個或jquery,我剛剛添加如果你想看看它。我將在未來使用<?php,我也是這樣使用的,但是大學的講師並沒有這麼做,於是我停了下來。 – ChrisSherwood 2012-01-04 20:04:24

+0

你會介意發佈的HTML以及?我首先傾向於相信$(「。usermsg」)。html(data);應該改變另一個元素的html。你的ajax可能會返回整個html,並將其添加到內部元素而不是容器中。如果是這種情況,改變你的php只返回發佈的評論,或者改變這個$(「。usermsg」)。html(data);到容器元件。在這種情況下,它不會重複,它只是將額外的html添加到加載的頁面。 – 2012-01-04 20:11:40

回答

1

我會先傾向於相信

$(".usermsg").html(data); 

應改變另一個元素的HTML。你的ajax可能會返回整個html,並將其添加到內部元素而不是容器中。如果是這種情況,請修改您的php以僅返回已發佈的評論,或將此更改爲容器元素的

$(".usermsg").html(data); 

。在這種情況下,它不會重複,它只是將額外的html添加到加載的頁面。作爲一個便箋,最好替換整個註釋塊 - 這取決於你如何處理頁面 - 而不是隻放置在添加的註釋中。這樣你的html會更新,如果另一個用戶碰巧也在評論。它會保持您的評論儘可能最新。 如果您分頁,恰好在某個內頁上,並且添加了足夠多的其他評論以將用戶推送到另一頁評論,則會有所不同。不過這是另一回事。

1

我相信你尋找ON DUPLICATE... UPDATE語法。結合一個獨特的索引(comment_pid, post.pidcomment_pid, user.uid?),這將防止愚蠢的發生,並且只需更新一行(相對於插入一個新行),如果已經存在相同的密鑰。

+0

我已經添加了jquery,只是檢查你的答案傢伙非常感謝! – ChrisSherwood 2012-01-04 20:01:56

0

你檢查過重複的地方嗎?插入/檢索文章+評論的服務器端腳本可能完美地工作,並且您的客戶端JavaScript正在通過在進行ajax調用之前從頁面中提取太多來導致重複。