0

I have a problem with my comment system. At the moment it is just simple adding to database sorting and showing under right post on the blog. I am creating, I will implement ajax etc later.

The issue I am having right now is that it shows the last comment on the post below the one I added comment on. So lets say I am adding comment on post 10, it will show comment on post 10 and 11. It sorts correctly, but it duplicates to the post below too. (It doesn't duplicate in database just the way it displays.)

  <?php

      // Connect to the database
      include('../acp/db/db.php');
          $link = mysql_connect($dbhost, $dbuser, $dbpassword, $dbname);
          mysql_select_db($dbname);
          if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
           $result = mysql_query('SELECT * FROM `posts` ORDER BY id DESC') or die(mysql_error());

              while($row = mysql_fetch_array($result)) {
                $id_post = $row['id'];
               echo " <!-- Blog Post Content Column -->
                    <h1> " . $row['post_title'] . " </h1><p class='lead'>
                    by <a href='#'>Matt</a></p>   <hr>
                     <p><span class='glyphicon glyphicon-time'>" . $row['date_created'] . "</span></p>
                     <img class='img-responsive' style='width: 900px;height: 300px;' src=" . $row['post_img'] . "> <hr>
                        <p class='lead'>" . $row['post_first'] . "</p>
                        <p>" . $row['post_second'] . "</p> <hr>

                        <!-- Comments Form -->
                        <div class='well'>
                            <h4>Leave a Comment:</h4>
                                <form id='form'method='POST'action='php/insert-comment.php'>
                                    <input type='hidden' name='post_id' value='$id_post'>
                                    <input type='text' id='comment-name' name='name' placeholder='Your name' />
                                    <input type='text' id='comment-mail' name='mail'  placeholder='Your e-mail adress' />
                                    <textarea type='text' name='comment' class='the-new-com' rows='3'></textarea>
                                    <input type='submit' id='submit' class='bt-add-com' value='Submit Comment'></input>
                                </form>
                        </div>

                        <hr>
                        <div class='media comment-block'>
                              <a class='pull-left' href='#'>
                                  <img class='media-object' src=' $grav_url' >
                              </a>
                              <div class='media-body'>$name
                                  <h4 class='media-heading'>
                                      <small>$date</small>
                                  </h4>
                                    $comment
                              </div>
                          </div>";

                     $resultcomments = mysql_query("SELECT * FROM `comment` WHERE post_id = '$id_post'") or die(mysql_error());
                        while($affcom = mysql_fetch_assoc($resultcomments)){
                            $name = $affcom['name'];
                            $email = $affcom['mail'];
                            $comment = $affcom['comment'];
                            $date = $affcom['date'];

                            $default = "mm";
                            $size = 35;
                            $grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;

                          echo"
                        <!-- Posted Comments -->

                        <!-- Comment -->
                      <div class='media comment-block'>
                            <a class='pull-left' href='#'>
                                <img class='media-object' src=' $grav_url' >
                            </a>
                            <div class='media-body'>$name
                                <h4 class='media-heading'>
                                    <small>$date</small>
                                </h4>
                                  $comment
                            </div>
                        </div>";
                      }
                    }
?>

I am slowly learning PHP so be easy on me. I feel like the issue is easy to fix, and it is right there I just cannot work out what it is.

Neil Lunn
  • 140,271
  • 35
  • 313
  • 302
  • 2
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde May 28 '17 at 23:44
  • Your acceptance record is questionable. You were kindly asked in [this comment](https://stackoverflow.com/questions/43260229/google-analytics-display-more-than-1-query#comment74776616_43261438) I left under another of your questions to accept the answer; it wasn't. Now, you come back for more help. – Funk Forty Niner May 28 '17 at 23:48
  • @Fred-ii- sorry I didn't know I have to accept answers, I just did it – Mateusz Kolacki May 28 '17 at 23:51
  • 1
    Btw this `$link = mysql_connect($dbhost, $dbuser, $dbpassword, $dbname);` doesn't do what you think or hope it does. In a nutshell; it failed. I can't possibly see how that would connect. `mysqli_` uses 4 arguments, not `mysql_` to connect with. – Funk Forty Niner May 28 '17 at 23:51
  • far as I can tell, you have most of your code wrapped inside this loop `while($row = mysql_fetch_array($result))` and I don't know why. That closing brace should be after your `";` just above `$resultcomments = mysql_query("SELECT * FROM comment...` - besides that; I don't know. – Funk Forty Niner May 29 '17 at 00:06
  • @Fred-ii- I know, the thing is when I did that, they didn't display at all :/ – Mateusz Kolacki May 29 '17 at 00:11

0 Answers0