-2

送信された内容を表示するフォームの作成方法を学んでいます。フォームに情報を送信すると、情報が 2 回表示され、理由がわかりません。私はまだこれに慣れていないため、私のコードには多くのエラーがある可能性があります。なぜそれが2回表示されるのかを確認するためにすべてを再確認していますが、問題を見つけることができないようです.

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

?>

ありがとう。

4

2 に答える 2

-1

この行を削除してみてください:

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>"; // Remove This
 // array_sum
 } 

while文の外に置く

于 2013-05-01T17:35:29.297 に答える