0

What problem i am having right now, is that there is a while loop to post my topics retrieved from MySQL. Works perfectly fine, until I get into inputting data. I have recently created a comment system, where for each topic there will be a comment box to submit. The problem is the while loop runs it over and over again, so when i type a comment for one topic, it posts to all of them. Here is my code:

//MYSQLI LOGIN DETAILS
        $servername = "***";
        $username = "***";
        $password = "***";
        $dbname = "***";
        //MYSQLI CREATE CONNECTION
        $constatus = new mysqli($servername, $username, $password, $dbname);
        //MYSQLI CHECK CONNECTION
        if ($constatus->connect_error) {
            die("Connection failed: " . $constatus->connect_error);
        }
        //MYSQLI COUNT COLUMNS
        $sql = "SELECT NEWSID, AUTHOR, ADMINSTS, DATE, HEADING, ARTICLE FROM news ORDER BY NEWSID DESC";
        $result = $constatus->query($sql);

        if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
                echo 
                "<div class=newsboard_topic>" .
                    "<div class=newsboard_authordate>" . $row["AUTHOR"];
                if ($row["ADMINSTS"] == admin) {
                    echo
                    "<div class=newsboard_adminfx>
                        Admin
                    </div>";
                } else if ($row["ADMINSTS"] == sadmin) {
                    echo
                    "<div class=newsboard_sadminfx>
                        Super Admin
                    </div>";
                }
                if ($_SESSION['adminsts'] == 'admin' || $_SESSION['adminsts'] == 'sadmin') {
                    echo "<span class=newsboard_adminactions> <img src='/image/remove.png' style='width:20px; height:20px;'> </span>";
                }
                echo
                    "<span class=date>" . $row["DATE"] .
                    "</span></div>
                    <h1>" . $row["HEADING"]. 
                    "</h1><p class=newsboard_topic_article>" .
                        $row["ARTICLE"] .
                    "</p>";
                $sqlcomments = "SELECT newscomments.USERID, newscomments.COMMENT, userdata.FIRSTNAME, userdata.LASTNAME, userdata.ADMINSTATUS FROM newscomments JOIN userdata ON newscomments.USERID=userdata.ID WHERE NEWSID=$row[NEWSID] ORDER BY COMMENTID DESC";
                $resultcomments = $constatus->query($sqlcomments);
                echo "<div class=newsboard_comments>
                    Comments
                    <br>";
                while($rowcomments = $resultcomments->fetch_assoc()) {
                    echo $rowcomments["FIRSTNAME"] . " " . $rowcomments["LASTNAME"] . " " . $rowcomments["COMMENT"] . "<br>";
                }
                if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
                    echo '
                        <form method="post">
                            <input class=postheadline type="text" name="comment" />
                            <input class=submit type="submit" id="submit" name="submit" value="Comment"/>
                        </form>';
                    if (isset($_POST[submit])) {
                        if (!empty($_POST[comment])) {
                            $sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$row[NEWSID]', '$_SESSION[profileid]', '$_POST[comment]')";
                            if ($constatus->query($sqlcommentpost) === TRUE) {
                                echo "Posted Successfully!";
                                break;
                            } else {
                                echo "Fatal Error. Please try again";
                                break;
                            }
                        }
                    }
                }
                echo "</div></div>"; /*Ends newsboard_topic Div & newsboard_comments Div*/
            }
        } else {
            echo "0 results";
        }

Live example is online at www.geovillageva.com however you cannot see comments as it is for registered members only because it will have a name for posters.

Rav
  • 1,327
  • 3
  • 18
  • 32
George Jones
  • 245
  • 2
  • 11
  • If I understand you correctly, add a hidden field for each comment box that defines which news post it belongs to, then just check with PHP and only add the comment to the one post. (think this is what you want?) – Enstage Nov 25 '16 at 04:30

3 Answers3

0

So, your input block

        if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
                echo '
                    <form method="post">
                        <input class=postheadline type="text" name="comment" />
                        <input class=submit type="submit" id="submit" name="submit" value="Comment"/>
                    </form>';
                if (isset($_POST[submit])) {
                    if (!empty($_POST[comment])) {
                        $sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$row[NEWSID]', '$_SESSION[profileid]', '$_POST[comment]')";
                        if ($constatus->query($sqlcommentpost) === TRUE) {
                            echo "Posted Successfully!";
                            break;
                        } else {
                            echo "Fatal Error. Please try again";
                            break;
                        }
                    }
                }
            }

needs to go by itself before the display loop. On the query inserting using $row[NEWSID], you need to use $_POST['newsid'] instead (and you may need to add that to your form to be posted along with the comments).

Please note that you need to beef up the security on this considerably or you will be hacked.

Community
  • 1
  • 1
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
0

You can try a .reset() on your form after the submission was successful(before the break).

sonic_4vi
  • 31
  • 1
  • 5
0

Lets include the news id also in the form. You can have a hidden input field for this. Then use this news id $_POST[nid] while inserting.

     if (isset($_SESSION['loggedon']) && $_SESSION['loggedon'] == true) {
                        echo '
                            <form method="post">
    <input class=postheadline type="text" name="comment" />
    <input type="hidden" name="nid" value="'.$row[NEWSID].'" />
    <input class=submit type="submit" id="submit" name="submit" value="Comment"/>
                            </form>';
              if (isset($_POST[submit])) {
                   if (!empty($_POST[comment])) {
  $sqlcommentpost = "INSERT INTO newscomments (NEWSID, USERID, COMMENT) VALUES ('$_POST[nid]', '$_SESSION[profileid]', '$_POST[comment]')";
                                if ($constatus->query($sqlcommentpost) === TRUE) {
                                    echo "Posted Successfully!";
                                    break;
                                } else {
                                    echo "Fatal Error. Please try again";
                                    break;
                                }
                            }
                        }
                    }
jophab
  • 5,356
  • 14
  • 41
  • 60