使用ajax支持的社交网络复制mysql注释条目


duplicate mysql comment entries with ajax powered social network?

正在尝试解决错误!我有一个ajax支持的社交网络(包括点赞和评论等)。我有一个工作评论脚本,但当我测试它时,我会得到原始帖子和评论的副本。不是只在帖子中添加评论,而是在每次输出数据时添加评论并复制帖子和评论。

我使用了jquery for ajax,它可以很好地工作,并且使用PHP,这可能是我刚接触jquery和PHP时希望得到一些帮助的显而易见的东西?很抱歉,如果需要,我可以减少代码的数量

谢谢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>
-->

我首先倾向于相信

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

应该更改另一个元素的html。ajax可能会返回整个html,并将其添加到一个内部元素,而不是容器中。如果是这种情况,请更改php以仅返回已发布的注释,或者更改此

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

到容器元件。在这种情况下,它不是复制,只是向加载的页面添加额外的html。

顺便说一句,替换整个评论块(取决于您处理页面的方式)肯定比只放在添加的评论中要好。这样,如果另一个用户碰巧也在评论,你的html就会更新。它将使您的评论尽可能保持最新。如果你正在分页,碰巧在某个内部页面上,并且添加了足够多的其他评论,将用户推到另一个评论页面,那就没什么不同了。不过那是另一回事了。

我相信您正在寻找ON DUPLICATE。。。UPDATE语法。与唯一索引(comment_pid, post.pidcomment_pid, user.uid?)相结合,这将防止重复发生,并且仅在具有相同密钥的行已经存在时更新行(而不是插入新行)。

您检查过重复发生的位置吗?插入/检索posts+注释的服务器端脚本可能工作得很好,而您的客户端javascript在执行ajax调用之前从页面中提取了太多内容,从而导致重复。