[英]For every submission, first inserted row is duplicated
我正在创建评论表单。 每次我提交表单时,都会添加第一行。 我试图用标题和退出来解决它。 但这也不行。 有人可以指导我吗?
<?php
$reizen = new reizen;
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$comment = $_POST['comment'];
$reizen -> comment($username, $comment);
header("Location: index.php?". $_SERVER['QUERY_STRING']);
exit();
}
echo $reizen -> retrieve();
$output = '';
$output .= '<html>';
$output .= '<head>';
$output .= '<link rel="stylesheet" href="css/main.css" type="text/css"/>';
$output .= '</head>';
$output .= '<body>';
$output .= '<div id="contactform">';
$output .= '<form name="form" id="form" action="index.php?page=review" method="post">';
$output .= '<label>Name:</label>';
$output .= '<input type="text" name="username" />';
$output .= '<label>comment</label>';
$output .= '<textarea name="comment" rows="20" cols="20"></textarea>';
$output .= '<label><img src="captcha.php"></label>';
$output .= '<input type="text" name="code">';
$output .= '<input type="submit" class="submit" name="submit" value="Send message" />';
$output .= '</form>';
$output .= '</body>';
$output .= '</div>';
$output .= '</html>';
echo $output;
?>
公共功能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 "iets gaat mis met inserten";
}
}
else
{
echo "missing fields";
}
}
public function retrieve()
{
if($retrieve = $this->db -> query("SELECT username, comment, time FROM reviews ORDER BY time LIMIT 5"))
{
while($row = $retrieve -> fetch_assoc())
{
$output .= '<div class="comment">';
$output .= '<div class="name">'. $row['username'] .'</div>';
$output .= '<div class="date">Added at '. date('H:i \o\n d M Y', strtotime($row['time'])) .'"></div>';
$output .= '<p>'. $row['comment'] .'</p>';
$output .= '</div>';
}
}
else
{
$output .= "iets gaat mis met retrieven";
}
return $output;
}
我认为这可能是一个问题:
$reizen->comment($username, $comment);
header("Location: index.php?". $_SERVER['QUERY_STRING']);
$ reizen-> comment($ username,$ comment); 发送回显到您的输出缓冲区。 您的标头将不会发送...标头必须始终放置在任何输出之前。
所以尝试
header("Location: index.php?". $_SERVER['QUERY_STRING']);
$reizen->comment($username, $comment);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.