我正在创建评论表单。每次我提交表单时,第一个添加的行都是重复的。我试着用标题解决它并退出。但这也行不通。有人可以指导我吗?
<?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;
?>
公共功能评论($ 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;
}
答案 0 :(得分:1)
我认为这可能是一个问题:
$reizen->comment($username, $comment);
header("Location: index.php?". $_SERVER['QUERY_STRING']);
$ reizen-&gt;评论($ username,$ comment);将echo发送到输出缓冲区。 你的标题不会发送... 标题必须始终放在任何输出之前。
所以试试
header("Location: index.php?". $_SERVER['QUERY_STRING']);
$reizen->comment($username, $comment);