我正在學習如何製作一個顯示提交內容的表單。當我向表單提交信息時,信息顯示兩次,我不知道爲什麼。我的代碼可能有很多錯誤,因爲我仍然是一個noob這個。我仔細檢查了一切,看看它爲什麼顯示兩次,我似乎無法找到問題。另一雙眼睛可以檢查我的php代碼,看看爲什麼當某人向表單中提交了某些內容時,它會顯示兩次?
<?php
$mysqli = new mysqli("localhost","root", "", "hits");
if(!$mysqli){
die('Could not connect: ' . mysqli_connect_error());
}
$db_selected = mysqli_select_db($mysqli, "hits");
if(!$db_selected){
die('can not use' . "hits" . ': ' . mysqli_connect_error());
}
$hit = $_POST['hit'];
$amount = $_POST['amount'];
$category = $_POST['category'];
$result = mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')");
if(!mysqli_query($mysqli, "INSERT into hit (hit, amount, category) VALUES ('$hit', '$amount', '$category')")){
die('Error: ' . mysql_Error());
}
$data = $mysqli->query("SELECT * FROM hit");
while($row = $data->fetch_assoc()) {
Print "<tr>";
Print "<th>Hit:</th> <td>".$row['hit'] . "</td> ";
Print "<th>Amount:</th> <td>".$row['amount'] . " </td>";
Print "<th>Category:</th> <td>".$row['category'] . "</td></tr> ";
Print "<br><br/>";
Print "</table>";
// array_sum
}
?>
謝謝。
問計一雙眼睛外面是絕對是一個「太本地化問題」的情況。無論如何,程序員應該運行代碼,而不是*監視它。 – 2013-05-01 17:30:40
您正在執行兩次INSERT查詢...而且您的代碼也非常脆弱,永遠不會將從遠程接收的值插入數據庫,而無需檢查/清理/清理! – fvu 2013-05-01 17:30:49
你插入了兩次,所以它正在顯示兩次 – Afsar 2013-05-01 17:30:53