I'm creating a small comment system, which displays all comments above each other. Problem is that each time I press the submit button the first row added in the database is duplicated. Which results in displaying alot of the same comments. I already try using header and exit, but that didn't worked out either. Could it be maybe have to do with the form action? Thanks in advance.
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";
}
}
Here is the usage, The form is echo'd below this, as you can see, I redirect to avoid duplication and then exit
$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();
您需要使用POST / REDIRECT / GET模式来防止这种情况。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.