我的数据库中有重复的评论,我该如何解决?

我有一些问题,每次刷新页面时它都会复制最后一条评论。我尝试使用重定向命令:


header("location:../../index.php");

但它给了我这个错误:


警告:无法修改标头信息 - 标头已发送(输出开始于 /home/timpovodnik/gymshark.timpovodnik.xyz/header.php:87)在 /home/timpovodnik/gymshark.timpovodnik.xyz/PHP/comments.php 上第 9 行。


这是我的代码:


<?php

    function setComments($conn) { // Funkcija, ki jo kasneje kličem v LoseFat in GainMuscles datoteki //zraven dodamo se spremenljivko $conn, ki je iz datoteke baza.php

        if(isset($_POST['commentSubmit'])) { // Če kliknemo na gumb zažene kodo

            $uid = $_POST['uid'];

            $date = $_POST['date'];

            $message = $_POST['message'];

            $sql = "INSERT INTO komentarji (uime, sporocilo, datum, uporabnik_id, program_id, objava_id) VALUES ('$uid', '$message', '$date', '1', '1', '1')";

            $result = mysqli_query($conn, $sql); // IZVEDE v BAZI

            header("location:../index.php");

            exit();

        }

    }


    function getComments($conn) {

        $sql = "SELECT * FROM komentarji";

        $result = mysqli_query($conn, $sql); // IZVEDE v BAZI

        while ($row = mysqli_fetch_assoc($result)) { // da v tabelo in jo shrani v $row

            echo "<div class='row'>";

                echo "<div class='comment-box'>";

                    echo $row['uime']."<br>";

                    echo $row['datum']."<br>";

                    echo nl2br($row['sporocilo']);

                echo "</div>";

            echo "</div>";

        }

    }

?>


倚天杖
浏览 112回答 1
1回答

慕虎7371278

我修复了它并向其添加了 Mysqli 函数。谢谢你的所有建议。<?phpinclude 'baza.php';&nbsp; &nbsp; if(isset($_POST['commentSubmit'])) { // Če kliknemo na gumb zažene kodo&nbsp; &nbsp; &nbsp; &nbsp; $uid = $_POST['uid'];&nbsp; &nbsp; &nbsp; &nbsp; $date = $_POST['date'];&nbsp; &nbsp; &nbsp; &nbsp; $message = $_POST['message'];&nbsp; &nbsp; &nbsp; &nbsp; $sql = "INSERT INTO komentarji (uime, sporocilo, datum) VALUES (?, ?, ?);";&nbsp; &nbsp; &nbsp; &nbsp; $stmt = mysqli_stmt_init($conn);&nbsp; &nbsp; &nbsp; &nbsp; if (!mysqli_stmt_prepare($stmt, $sql)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../loseFat.php?error=sqlerror"); // Če stavek ne dela&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_bind_param($stmt, "sss", $uid, $message, $date);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_stmt_execute($stmt); // Executa stavek v DB&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header("Location: ../loseFat.php?comment=success");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }function getComments($conn) {&nbsp; &nbsp; $sql = "SELECT * FROM komentarji";&nbsp; &nbsp; $result = mysqli_query($conn, $sql); // IZVEDE v BAZI&nbsp; &nbsp; while ($row = mysqli_fetch_assoc($result)) { // da v tabelo in jo shrani v $row&nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='row'>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<div class='comment-box'>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row['uime']."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $row['datum']."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo nl2br($row['sporocilo']);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; &nbsp; &nbsp; echo "</div>";&nbsp; &nbsp; }}?>
打开App,查看更多内容
随时随地看视频慕课网APP