0

我正在创建一个评论表单。每次我提交表单时,第一个添加的行都会重复。我试图用标题解决它并退出。但这也行不通。有人可以指导我吗?

<?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;
}
4

1 回答 1

1

我认为这可能是一个问题:

$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);
于 2013-02-23T20:14:22.640 回答