csj89
csj89

Reputation: 3

MySQL Table inserts empty records before data

I'm a beginner at this. Basically I have a form that has the correct names set up, and it sends the data to the MySQL table correctly, but it always inserts an additional 4 blank rows/ID's whenever I execute the form.

Here's the result every time: https://i.sstatic.net/bnx5D.png

I'm not sure whether if there's something wrong with the code or the setup itself, can anybody help?

PHP code:

<?php
$email = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$member_company = $_POST['member_company'];
$member_address1 = $_POST['member_address1'];
$member_address2 = $_POST['member_address2'];
$member_city = $_POST['member_city'];
$member_country = $_POST['member_country'];
$member_post_code = $_POST['member_post_code'];
$member_phone = $_POST['member_phone'];

mysql_connect ("localhost", "username", "password") or die ('Error: ' . mysql_error());
mysql_select_db ("database");

$query="INSERT INTO Orders (ID, email, first_name, last_name, member_company, member_address1, member_address2, member_city, member_country, member_post_code, member_phone) 
VALUES ('NULL', '".$email."', '".$first_name."', '".$last_name."', '".$member_company."', '".$member_address1."', '".$member_address2."', '".$member_city."', '".$member_country."', '".$member_post_code."', '".$member_phone."')";

mysql_query($query) or die ('Error updating database');

?>

Upvotes: 0

Views: 606

Answers (1)

Lombo
Lombo

Reputation: 12235

The thing is that every time you do a request you save the data on the table.

You should check if it is a POST request

if(isset($_POST){
    //Then the form has been submitted
}

This is running on every GET request also, that's why you get those empty records.

Upvotes: 1

Related Questions