Php 对于每个提交,第一个插入的行都是重复的
我正在创建一个评论表单。每次提交表单时,添加的第一行都会重复。我试着用header和exit来解决这个问题。但这也行不通。有人能给我引路吗Php 对于每个提交,第一个插入的行都是重复的,php,header,Php,Header,我正在创建一个评论表单。每次提交表单时,添加的第一行都会重复。我试着用header和exit来解决这个问题。但这也行不通。有人能给我引路吗 <?php $reizen = new reizen; if(isset($_POST['submit'])) { $username = $_POST['username']; $comment = $_POST['comment']; $reizen -> comme
<?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;
?>
}
公共函数检索()
{
如果($retrieve=$this->db->query(“按时间限制5选择用户名、注释、来自审查订单的时间”))
{
而($row=$retrieve->fetch_assoc())
{
$output.='';
$output.=''.$row['username'].';
$output.='Added at'.date('H:i\o\n d M Y',strottime($row['time']))。“>”;
$output.=''.$row['comment'].';
$output.='';
}
}
其他的
{
$output.=“iets gaat mis met检索”;
}
返回$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);
您的代码看起来很好…我想说还有其他原因在起作用。可能您的页面被加载了两次或更多。请确保您不是预取或其他原因的受害者。这可能是由于表单中的操作造成的吗?可能-您在何处使用$\u GET变量
page=review
?您可能正在加载某些内容。相反,我应该做什么让我们先阻止$reizen->comment
向屏幕发送任何内容。如果你的答案有效,那么他应该重写他的comment
函数。我同意。我认为他会使用2个echo进行调试,但我认为它们不是必需的。而且名字不错,Hansbest先生;)为什么这行:$insert->bind_param('sss',$username,$comment,$date);
$reizen->comment($username, $comment);
header("Location: index.php?". $_SERVER['QUERY_STRING']);
header("Location: index.php?". $_SERVER['QUERY_STRING']);
$reizen->comment($username, $comment);