我制作了一个简单的评论系统。当我重新加载页面时,第一次插入的评论被复制了。我应该使用标头通过 request_uri 重定向吗?我这样做了,它变成了一个标题循环。提前致谢
public function comment($username, $comment, $code)
{
if(!empty($username) && !empty($comment) && !empty($code))
{
$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 id LIMIT 0, 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>';
}
return $output;
}
}
<?php
$reizen= new reizen;
echo $reizen->retrieve();
?>
<script type="text/javascript">
</script>
<?php
$output = '';
$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;
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$comment = $_POST['comment'];
$code = $_POST['code'];
$reizen->comment($username, $comment, $code);
}
?>