Robert Keith
Robert Keith

Reputation: 3

My form is inserting two records into my mysql table instead of one

I have a form that, onsubmit, inserts the data into a table in my database. It works fine except that it inserts two instances of data instead of one. The first instance contains all the info from the form. The second instance contains blank info. My javascript validation is working fine. Here is a trimmed down version of my code after connection :

// escape variables for security
$fname = mysqli_real_escape_string($con, $_POST['fname']);
$lname = mysqli_real_escape_string($con, $_POST['lname']);

$sql="INSERT INTO Persons (fname, lname) VALUES ('$fname', '$lname')";


if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}


mysqli_close($con);
?>

When I check my table in phpmyadmin, it contains 2 records, populated data and blank rows. Any thoughts?

Upvotes: 0

Views: 109

Answers (2)

SukieC
SukieC

Reputation: 75

using <img src="" width="some number" height="some number"> to force a blank table cell in HTML causes a similar problem with Apache and MySQL.

Upvotes: 0

SukieC
SukieC

Reputation: 75

The variable $fname is declared twice, consequently overwriting itself. Thus the blank second instance.

Upvotes: 1

Related Questions