我正在創建評論表單。每次提交表單時,第一個添加的行都會被複制。我試着用header和exit來解決它。但那也行不通。有人能指導我嗎?對於每次提交,首先插入的行都是重複的
<?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;
?>
公共職能評論($的用戶名,$評論) {
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;
}
您的代碼看起來不錯...我會說些什麼在作怪。也許你的頁面正在被加載兩次或什麼東西。確保你不是預取或其他東西的受害者。 – 2013-02-23 20:00:29
是否可能是由於表單中的操作而引起的? – HansWorst 2013-02-23 20:06:00
也許 - 你在哪裏使用$ _GET變量'page = review'? – 2013-02-23 20:10:50