Can another pair of eyes check my php code to see why when someone submits something into the form, it gets displayed twice?

I'm learning how to make a form that displays what's submitted. When I submit information into the form, the info displays twice and I am not sure why. My code may have a lot of errors because i'm still a noob with this. I'm double checking everything to see why it is getting displayed twice and I just can't seem to find the problem.

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

?>

Thanks.

Answers


You run the query twice.

$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());
}

I think you mean:

if( ! $result ){
    die('Error: ' . mysql_Error());
}

I would change to using PDO for databases though.


Need Your Help


About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.