Repeating comment when refreshed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Toxicous8
    New Member
    • Oct 2015
    • 57

    Repeating comment when refreshed

    I have a problem.I made a very usable comment section with a database called comment.But when I refresh the page or open it again it copies the comment that I typed for testing.How do I solve that problem.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Have you checked the code that is being ran on the page-load?

    Comment

    • Toxicous8
      New Member
      • Oct 2015
      • 57

      #3
      YES!Over and over again.But I don't see anything suspicious.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Well, it's a little hard for us to say what's wrong without any code to look at.

        Comment

        • Toxicous8
          New Member
          • Oct 2015
          • 57

          #5
          Okay then, here are the codes.
          Code:
          <form action="<?php $_PHP_SELF ?>" method="POST">
          Name: <input type="text" name="name" required="required" placeholder="eg. tox!c or benz" /><br/><br/>
          E-mail: <input type="text"  name="email" required="required" placeholder="eg. toxic5@gmail.com" /><br/><br/>
          Comments : <br/>
          <textarea required="required" title="Comments" rows="5" cols="50" name="comment" placeholder="Any comment welcomed...">
          </textarea>
          <input style="display: inline-block; background:transparent; max-width: 200px;  margin: 50px 10px 30px 10px;  padding: 10px;  color: #000;  border:1px solid #000; cursor:pointer; text-align: center;" type="submit" name="submit" value="Leave comment" onclick="Thank();"/>
          </form>
          
          <div style="background:lavenderblush;max-width:60em;position:relative; left:30px;box-shadow:2px 2px 2px #aaa;">
          <div style="background:#e9967a;"><br/></div>
          <h1>Comments</h1>
          
          
          <?
          include("connect.php");
          if(isset($_POST['submit']))
          {
          $e=$_POST['email'];
          $n=$_POST['name'];
          $c=$_POST['comment'];
          $insert=mysql_query("use codb");
          $insert=mysql_query("insert into comment
          (name,email,comment)
          values ('$n','$e','$c')")or die(mysql_error());
          }
          $select = mysql_query("select * from comment" );
          while ( $row = mysql_fetch_array ( $select )) {
          echo '<h3 class="h3">'.$row [ 'name' ].'</h3>';
          echo '<i>'.$row [ 'comment' ].'</i><br/>';
          
          echo "<br /><hr/>"; 
          }
          
          ?>

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            How come you're inserting into table comment but reading from table comment4?

            Comment

            • Toxicous8
              New Member
              • Oct 2015
              • 57

              #7
              It was just a mistake I made when pasting it.So I'm guessing the problem is that every time the page loads, the code fetches the data from the database and echo it.Now how do I make the php codes not to duplicate it after reloading???

              Comment

              • Toxicous8
                New Member
                • Oct 2015
                • 57

                #8
                Hi again, i've updated the codes to the comment section but still repeating. The newer codes are
                Code:
                 <!--   HTML FORM   -->
                
                
                
                <h3>Leave a comment</h3>
                
                <form action="<?php $_PHP_SELF ?>" method="POST">
                Name: <input type="text" name="name" required="required" placeholder="eg. tox!c or benz" maxlength="25" /><br/><br/>
                
                	
                
                
                
                
                
                
                
                Comments : <br/>
                <textarea required="required" title="Comments" rows="10" cols="50" name="comment" placeholder="Any comment welcomed...">
                </textarea>
                
                <input style="display: inline-block; background:transparent; max-width: 200px;  margin: 50px 10px 30px 10px;  padding: 10px;  color: #000;  border:1px solid #000; cursor:pointer; text-align: center;" type="submit" name="submit" value="Comment" />
                </form>
                
                
                
                <div style="background:#fff0f5; max-width:80%; margin-left:7%; box-shadow:2px 2px 2px #aaa; color:#000;">
                <div style="background:#e9967a;"><br/></div>
                <h1>Comments</h1>
                
                
                
                
                
                
                <!--   INSERT COMMENT TO DATABASE   -->
                
                <?php
                include('connect.php');   #connect to CODB
                        #Assign variables 
                $name = $_POST['name'];
                $ip = $_SERVER['REMOTE_ADDR'];
                $comment = $_POST['comment'];
                $submit = $_POST['submit'];
                
                
                #check if the user has input and store it in COMMENTS else echo out NOT INSERTED
                if( isset($submit) ) {
                $insert = @mysql_query("USE codb");
                $insert = @mysql_query("INSERT INTO comment (ip,name,comment) VALUES ('$ip','$name','$comment') ");
                echo '<span style="color:#000;background:rgba(30, 250, 30, 0.1);border:1px solid #0f0;">Inserted</span>';
                }else {
                    echo '<span style="color:#f00;background:rgba(250, 10, 10, 0.3);border:1px solid #f00;">Please fill out all the fields correctly</span>';
                }
                
                ?>
                
                
                <!--   PREVIEW COMMENTS   -->
                
                
                
                <?php
                include('connect.php');     #connect CODB
                        #Assign variables
                $s = date("d/m/y h:i:s");    #Assign the date of input
                $getquery = mysql_query("SELECT * FROM comment ORDER BY id DESC LIMIT 70")or die(mysql_error());     #get comments
                
                
                
                #Check if there are comments and preview them
                
                
                while ($rows = mysql_fetch_assoc($getquery))  {
                    $id = $rows['id'];
                    $s = $rows['submission_date'];
                    $name = $rows [ 'name' ];
                    $comm = $rows ['comment'];
                
                
                    echo "<br/>$id . <h3 style=\"font-size:25px;\">$name</h3>";
                    echo "<i>$s<br/><br/></i>";
                    echo "$comm<br/><br /><hr/>";
                }
                
                
                ?>

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I don't see any reason why the comment would keep inserting unless the user is constantly refreshing the page and resubmitting the data.

                  Comment

                  • Toxicous8
                    New Member
                    • Oct 2015
                    • 57

                    #10
                    Yeah, i've already figured that out. It inserts, fetches and displays the last comment when refreshed. I've read a blog about making comments and at the last part he said that he edited the codes because it was repeating and that he highlighted the line, but i didn't see any highlighted line, maybe because i was using my phone, and when i tried to ask him to write the codes on the comment the page displays blank as if the blog was deleted. Any idea how i can solve this problem??? tnx

                    Comment

                    • Toxicous8
                      New Member
                      • Oct 2015
                      • 57

                      #11
                      And I've also tried the
                      Code:
                      INSERT IGNORE ...
                      and
                      Code:
                      ON DUPLICATE KEY
                      but no success (the
                      Code:
                      ON DUPLICATE KEY
                      doesn't even insert the comments in the first place.

                      Comment

                      Working...