2013-02-23 27 views
0

我正在創建評論表單。每次提交表單時,第一個添加的行都會被複制。我試着用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; 
} 
+1

您的代碼看起來不錯...我會說些什麼在作怪。也許你的頁面正在被加載兩次或什麼東西。確保你不是預取或其他東西的受害者。 – 2013-02-23 20:00:29

+0

是否可能是由於表單中的操作而引起的? – HansWorst 2013-02-23 20:06:00

+0

也許 - 你在哪裏使用$ _GET變量'page = review'? – 2013-02-23 20:10:50

回答

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); 
+0

你可能會找到一些東西。相反,我要做的是阻止'$ reizen-> comment'發送任何東西到屏幕上。如果你的答案有效,那麼他應該重寫他的「評論」功能。 – 2013-02-23 20:16:38

+0

我同意。我認爲他會使用2回聲進行調試,但我認爲他們沒有必要。和好名字HansWorst先生;) – 2013-02-23 20:25:24

+0

仍然沒有工作,夥計們。我做了你告訴我的。 – HansWorst 2013-02-23 20:55:36