Php 使用ajax支持的社交网络复制mysql注释条目?
试图解决一个错误!我有一个ajax支持的社交网络(带有like和评论等)。我有一个工作评论脚本,但当我测试它时,我得到了一个原始帖子和评论的副本。它不是只在文章中添加注释,而是在每次输出数据时添加注释并复制文章和注释 我使用了jqueryforajax,它在jquery和PHP中工作得很好,可能是因为我刚刚接触jquery和PHP,所以我希望得到一些帮助?抱歉,如果需要,我可以减少代码数量 谢谢 jquery php在上传评论时给出双贴子Php 使用ajax支持的社交网络复制mysql注释条目?,php,jquery,mysql,Php,Jquery,Mysql,试图解决一个错误!我有一个ajax支持的社交网络(带有like和评论等)。我有一个工作评论脚本,但当我测试它时,我得到了一个原始帖子和评论的副本。它不是只在文章中添加注释,而是在每次输出数据时添加注释并复制文章和注释 我使用了jqueryforajax,它在jquery和PHP中工作得很好,可能是因为我刚刚接触jquery和PHP,所以我希望得到一些帮助?抱歉,如果需要,我可以减少代码数量 谢谢 jquery php在上传评论时给出双贴子 <? $sql = "SELE
<?
$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>
--> 我相信你在寻找语法。与唯一索引(
comment\u-pid,post.pid
或comment\u-pid,user.uid
?)相结合,可防止重复发生,并仅在已存在具有相同键的行时更新行(相对于插入新行)。是否检查了重复发生的位置?插入/检索帖子和评论的服务器端脚本可能工作得很好,而客户端javascript在执行ajax调用之前从页面中提取了太多内容,从而导致了重复。我首先倾向于相信
$(".usermsg").html(data);
应该更改另一个元素的html。您的ajax可能返回整个html并将其添加到一个内部元素而不是容器中。如果是这种情况,请更改php以仅返回已发布的注释,或者更改此设置
$(".usermsg").html(data);
到容器元素。在本例中,它不是复制,只是向加载的页面添加额外的html
作为旁注,替换整个注释块(取决于您如何处理页面)肯定比只放置添加的注释要好。这样,如果另一个用户碰巧也在评论,您的html就会更新。它将尽可能使您的评论保持最新。
如果您正在分页,恰好在某个内部页面上,并且添加了足够多的其他注释,从而将用户推到另一个注释页面,则不会有什么不同。不过这是另一回事。您没有显示更新代码或处理html更新的js。另外,请用正确的方法打开php,更新代码运行良好,我很确定这是this或jquery,如果你想看的话,我刚刚添加了jquery。我将使用你介意张贴的html以及?我首先倾向于相信$(“.usermsg”).html(数据);应该更改另一个元素的html。您的ajax可能返回整个html并将其添加到一个内部元素而不是容器中。如果是这种情况,请更改php以仅返回已发布的注释,或者更改此$(“.usermsg”).html(数据);到容器元素。在这种情况下,它不是复制,它只是向加载的页面添加额外的html。我想您已经拥有了它。html基本上就是上面的php,通过usermsg div中的帖子和评论循环,我的新数据正在替换它。因此,如果我以一个注释类为目标,并对其进行更新,那么应该可以做到这一点并停止重复。我在想这些事情,但想不出如何接近。非常感谢您的时间,Kai已经添加了jquery,只是查看了您的答案,伙计们,非常感谢!
$(".usermsg").html(data);