Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7034467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:22+00:00 2026-05-28T01:11:22+00:00

Okay so I thought I had solved this problem. But the solution still evades

  • 0

Okay so I thought I had solved this problem. But the solution still evades me!

I have a ajax powered social chat application allowing comments and likes etc built with php, mysql and jquery. But when I post a comment the ajax returns a duplicate post with comment updated, so when you comment again you end up with mutiple duplicate posts. The Msql entries are fine (ie no duplication). All my other functions and updates work fine, so this is a severe pain in the bum!

Apologies for the amount of code but I am just trying to give you all the information – all thoughts and answers much appreciated!

Here is the start page HTML:

 <html>

 <div class="usermsg">

   <?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)){?>



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


     ?>

         <p class="likepara">this post has <? echo $row['likes']; ?> likes  <a class="like" pid=<? echo $row['pid']; ?> href="#">like</a></p>    

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

   <?php

    $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 class="commentsheader">Comments</p><br>';
           echo 'This post has no comments yet, be the first!';
           echo '<br><br><br><hr><br><br>';

      }else{
           echo '<p class="commentsheader">Comments</p><br>';
     while($row=mysql_fetch_assoc($result_comments)){

       $formattime_comment = date("g:i:a",$row['comment_time']);
       echo '<p class="comment">'.$row['comment'].'<br><img class="commentprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/>Posted by '.$row['name']. ' at ' .$formattime_comment. '</p>';
       //echo '<img class="commentprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/>'

     }//$sql_comments

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


      }//else end



    }//$sql


    ?>




   </div><!--user msg--


   </html>

The jquery function:

   $(".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{
                    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);

                                }
                    });
            }
        });
  });

the update php (basically the same as the inital html page being updated).

    <?php

    include("connectdb.php");
    include_once("secure.php");



    //still repeating posts when mutiple comments added?
    $commentpostid = $_POST['postid'];
    $commentuserid = $_POST['commentuserid'];
    $comment = protect($_POST['comment']);
$time =time();

$comment = strip_tags($comment);
    $time =time();

$sql_insert_comments = "INSERT INTO comments
              (comment_uid, comment_pid, comment, comment_time)
              VALUES
              ($commentuserid,$commentpostid, '$comment', $time)";

$insert_comments_result = mysql_query($sql_insert_comments);

    ?>

     <?

    $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'].'<br><br><img class= "userprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/> Posted by '.$row['name'].' at '.$formattime. '</p>';

    //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>';
?>

    <p class="likepara">this post has <? echo $row['likes']; ?> likes  <a class="like" pid=<? echo $row['pid']; ?> href="#">like</a></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 class="commentsheader">Comments</p><br>';
                 echo 'This post has no comments yet, be the first!';
                 echo '<br><br><br><hr><br><br>';

            }else{
                  echo '<p class="commentsheader">Comments</p><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>';
                    echo '<p class="comment">'.$row['comment'].'<br><img class="commentprofileimage" src="'.$row['uid_imageurl'].'" alt="user profile image"/>Posted by '.$row['name']. ' at ' .$formattime_comment. '</p>';

            }//$sql_comments

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

      }//else end

    }//$sql


    ?>




    </div>









</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:11:23+00:00Added an answer on May 28, 2026 at 1:11 am

    I think you should get rid of the post rendering in the update.php.

    EDIT: I’m a bit lost in your code. As I see you’re completely rendering the post again and replace the original one.

    You should only update the comments, actually you don’t have to get any html response from the ajax request. A success/fail flag would be enough. You can put the new comment from the editable area to the bottom (or top) of the comment list on success.

    Update php:

    include("connectdb.php");
    include_once("secure.php");
    
    //still repeating posts when mutiple comments added?
    $commentpostid = protect($_POST['postid']);
    $commentuserid = protect($_POST['commentuserid']);
    $comment = protect($_POST['comment']);
    $time =time();
    
    $comment = strip_tags($comment);
    
    $query = "INSERT INTO comments
                (comment_uid, comment_pid, comment, comment_time)
              VALUES
                ($commentuserid ,$commentpostid, '$comment', $time)";
    
    if(mysql_query($sql_insert_comments))
    {
        echo "OK";
    }
    else
    {
        echo "FAIL";
    }
    

    You’ll get “OK” or “FAIL” as response data. So you can make the right move in JS:

    $(".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{
                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){
                                if(data == "OK") {
                                    //put comment into the comment list
                                } else {
                                    alert('error');
                                }
                             }
                });
            }
        });
    });
    

    You should slightly modify your comment list for this. You should put the comments into a container div, sou you’ll be able to append the new commit to the list with jQuery like

    $("#post_1 div.comment_container").append($("<div/>").text('comment text'))

    Also needed to store the posts in signed wrappers to be able to figure out which post’s comment list should be updated. So the structure of the page should look like this:

    <div class="post_list">
        <div class="post" id="post_1">
            <!-- post body -->
            <div class="comment_container">
                <!-- comments -->
            </div>
        </div>
    
        <div class="post" id="post_2">
            <!-- post body -->
            <div class="comment_container">
                <!-- comments -->
            </div>
        </div>
    
        <div class="post" id="post_3">
            <!-- post body -->
            <div class="comment_container">
                <!-- comments -->
            </div>
        </div>
        ...
    </div>
    

    EDIT: And the real solution would be to remove the “left join comments” from the query where you’re getting the post. The join multiplies the rows.

    For instance:
    You’ve got A, B post, with comments A <= a,b and B <= c,d,e.
    The result of the query would be 5 lines if I’m correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so this is not the first time I've had this problem, but it
I thought I would find a solution to this problem relatively easily, but here
Okay at first I thought this would be pretty straightforward. But I can't think
Okay so im working on this php image upload system but for some reason
Okay, so this probably sounds terribly nefarious, but I need such capabilities for my
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
Okay, this just feels plain nasty, but I've been directed to do it, and
Okay so here's the lowdown... been working on this solution for the past week
Okay, a little preface: I am still a beginner when it comes to AJAX,
I searched around for the answers to these questions, but I have had little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.