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

public function comment($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;
}
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 に答える