为什么PHP从一个数据条目向MySQL插入多行?

I have an HTML code that creates a template to collect 5 variables. These variables are posted to a php that process the info and forward it to a mySQL database.

Following is the HTML template:

<html>

<head>
</head>

<body>
    <form action="insert.php" method="post">
        <p>Value1 <input type="text" name="value1" /></p>
        <p>Value2 <input type="text" name="value2" /></p>
        <p>Value3 <input type="text" name="value3" /></p>
        <p>Value4 <input type="text" name="value4" /></p>
        <p>Value5 <input type="text" name="value5" /></p>

        <input type="Submit" />

    </form>
</body>

</html>

And here is the PHP form that interacts with MySQL (note that the # are substituted with the actual values in the original form):

<?php
$server="#####"; 
$username="#####";
$password="######";
$database="#####";


$val1=$_POST['value1'];
$val2=$_POST['value2'];
$val3=$_POST['value3'];
$val4=$_POST['value4'];
$val5=$_POST['value5'];

$con = new mysqli($server,$username,$password,$database);
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql= "INSERT INTO test_table (value1, value2, value3, value4, value5)     VALUES ('$val1','$val2','$val3','$val4','$val5')";

if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

mysqli_close($con);

?>

The two forms seem to work fine and there are no errors popping out. The problem is that when I switch to the mysql prompt and interrogate the database to see if it works it shows multiple entry rows instead of only one. I am pretty confident that the issue is not related to the database's table which was previously created. Just in case I am posting the code I used to create the table.

mysql> create table test_table (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                            value1 VARCHAR(10),
                            value2 VARCHAR(10),
                            value3 VARCHAR(10),
                            value4 VARCHAR(10),
                            value5 VARCHAR(10))
                            ;

So for example, if I load the html form and I insert "a" "b" "c" "d" and "e" in the fields the output should be only 1 record in the mysql. Instead I get two.(I cannot post a picture of the output cause I do not have enough experience point). I have been trying to fix that but I couldn't find where the problem is. Any help is greatly appreciated.

There's nothing in your code that would create a duplication. One form, if handled once by your PHP code would generate only one row on the db as you know, so either you are posting to a file and then importing another that handles the form more than once or you have some weird redirect going on.

For "a" "b" "c" "d" and "e" to be registered twice (two rows) the form contents are being sent twice to that PHP. First, try to find out if you are submitting only once: - In firefox install Live HTTP Headers, capture the POST and see if it goes just once; Are you using jquery to validate or handle the form submit? if so are you preventing the normal event to occur? If not you might get the data posted twice, one by ajax and the other by normal browser post.

When you refresh your form you still have your POST variables stored from previous entry. Refreshing your browser (in chrome as far as I know) resubmits your POST variables. That is why you seem to get double entries.

In the case you didn't find an answer for this, as none of the answers are marked as a solution, I was having the same issue on my machine today, but it was inserting two random rows and the actual row I was inserting. The bizarre thing is, I was getting multiple entries even giving a single insert instruction on the php interactive mode. Not satisfied, I went directly to mysql console to see what would happen, and, instead of getting one entry I was getting the same three as before. So, the problem was on MySQL and what I thought was trying to drop the table and create it again... It magically solved my problem.