【问题标题】:Duplicate Database entry after refreshing刷新后重复的数据库条目
【发布时间】:2020-01-05 05:08:40
【问题描述】:

我正在尝试创建一个数据库,用户使用 PHP 在其中输入他们对产品的评论。该函数确实将用户数据输入到数据库中,但每次刷新页面时它都会重复最后一个条目。我怎样才能让它停止重复?我试过标头(位置:index.php?reviewed);但它没有工作

<?php

function setComments($conn) {

if (isset($_POST['commentSubmit'])) {
    $uid = $_POST['uid'];
    $date = $_POST['date'];
    $message = $_POST['message'];



    $sql = "INSERT INTO comments (uid, date, message)
    VALUES ('$uid', '$date', '$message')";
    $result = $conn->query($sql);



}

}


function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
    echo "<div class = 'commentBox'>";
    echo $row['uid']." <br>";
    echo $row['date']." <br> ";
    echo nl2br($row['message']);
    echo "</div>";
}
}

【问题讨论】:

  • "我试过 header(Location: index.php?reviewed); 但没用" 那你一定是那里做错了,这就是要走的路.
  • 当我包含“header(Location: index.php?reviewed);”时,它仍然重复最后一个条目
  • 我对数据库不是很熟悉,这也是我第一次尝试用 PHP 编码,所以不确定我做错了什么。但是,我关注的视频没有包含标题声明,并且仍然没有重复。

标签: php mysql database forms


【解决方案1】:

您必须在使用header(Location: index.php?reviewed); 之前输入&lt;?php ob_start(); ?&gt;。我建议将ob_start() 放在第 1 行

例如:

<?php ob_start(); ?>

<?php function random(){} ?>

<?php header(Location: index.php?reviewed); ?>

【讨论】:

  • 所以标题会在两个函数之后还是在某个函数内部?对不起,我没有完全理解
  • ob_start() 应该放在其他任何东西之前。 header(Location: index.php?reviewed); 不起作用的原因是因为你前面没有ob_start()
猜你喜欢
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-16
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多