简体   繁体   English

对于每个提交,第一个插入的行都会重复

[英]For every submission, first inserted row is duplicated

I'm creating a comment form. 我正在创建评论表单。 Each time I submit the form the first added row is duplicated. 每次我提交表单时,都会添加第一行。 I've tryed to resolve it with header and exit. 我试图用标题和退出来解决它。 But that wouldn't work either. 但这也不行。 Could someone please guide me? 有人可以指导我吗?

<?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) { 公共功能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;
}

I think that this can be a problem: 我认为这可能是一个问题:

$reizen->comment($username, $comment);
header("Location: index.php?". $_SERVER['QUERY_STRING']);

$reizen->comment($username, $comment); $ reizen-> comment($ username,$ comment); sends an echo to your output buffer. 发送回显到您的输出缓冲区。 Your header won't be send... A header must be placed always BEFORE ANY output. 您的标头将不会发送...标头必须始终放置在任何输出之前。

So try 所以尝试

header("Location: index.php?". $_SERVER['QUERY_STRING']);
$reizen->comment($username, $comment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM