drnpwmq4536 2016-11-25 04:03
浏览 31
已采纳

MYSQL和PHP循环失败

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.

  • 写回答

3条回答 默认 最新

  • duanchi5078 2016-11-25 06:51
    关注

    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;
                                    }
                                }
                            }
                        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等