2012-01-04 50 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调用之前从页面中提取太多来导致重复。