每次刷新网页时,添加的第一行都是重复的

时间:2013-02-24 17:37:26

标签: php header duplicates

我正在创建一个小型评论系统,它会显示彼此之上的所有评论。问题是每次按下提交按钮时,数据库中添加的第一行都是重复的。这导致显示大量相同的评论。我已经尝试使用标题和退出,但这也没有成功。它可能与表单行为有关吗?提前致谢。

public function comment($username, $comment)
{
    if (!empty($username) && !empty($comment)) {
        $date = date("Y-m-d H:i:s");
        if ($insert = $this->db->prepare("INSERT INTO reviews (username, comment, time) VALUES (?, ?, ?)")) {
            $insert->bind_param('sss', $username, $comment, $date);
            $insert->execute();
        } else {
            echo "something is wrong with the query";
        }
    } else {
        echo "missing fields";
    }
}

以下是用法,表格在此下方回显,如您所见,我重定向以避免重复然后退出

$reizen = new reizen;
if (isset($_POST['submit'])) {
    $username = $_POST['username'];
    $comment  = $_POST['comment'];
    header("Location: index.php?" . $_SERVER['QUERY_STRING']);
    $reizen->comment($username, $comment);
    exit();
}
echo $reizen->retrieve();

1 个答案:

答案 0 :(得分:0)

您需要使用POST/REDIRECT/GET pattern来阻止此操作。