I am trying to create a simple mailing list signup form from scratch using php and MySQL. Essentially, users provide their e-mail address (along with other information), and then PHP sends the info into a MySQL database, so i can send a mailer based on the information gathered. Sounds simple enough right? well im a touch new to PHP and i am having this problem where everytime the page that the forms are on loads, the MySQL database receives an empty row. in other words, the database is receiving the "data" from a bunch of empty forms everytime the page loads. and then the database receives real data when a user clicks the submit button. SO i end up with real data interspersed with a bunch of empty rows. Can anyone take a look at my code, and tell me if they can figure out why this is happening?
im assuming i made a stupid n00b mistake. as this doesnt seem to be an overwhelming problem for many php developers.
all help sincerely appreciated.
Here is the HTML code for the form itself, incase the problem is in the form... i dont know.
Here is the PHP i am using to send the data gathered in the forms to the MySQL database.
Here is what the reslting table looks like in MySQL... all fields are empty except for "id" which cant be null.
im assuming i made a stupid n00b mistake. as this doesnt seem to be an overwhelming problem for many php developers.
all help sincerely appreciated.
Here is the HTML code for the form itself, incase the problem is in the form... i dont know.
HTML:
<p>
<table cellspacing=2 cellpadding=0 border=0>
<tr>
<td>
<form action='mailinglist.php?register=yes' method=POST>
First Name:
</td>
<td>
<input type=text name='firstname' size=20 maxlength=30>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type=text name='lastname' size=20 maxlength=30>
</td>
</tr>
<tr>
<td>
City, State:
</td>
<td>
<input type=text name='city' size=15 maxlength=30>, <input type=text name='state' size=2 maxlength=2>
</td>
</tr>
<tr>
<td>
E-mail:
</td>
<td>
<input type=text name='email' size=20 maxlength=30>
</td>
</tr>
<tr>
<td>
<input type=submit value="Register!">
</form>
</td>
</tr>
</table>
</p>
Here is the PHP i am using to send the data gathered in the forms to the MySQL database.
PHP:
<?php
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$city=$_POST['city'];
$state=$_POST['state'];
$email=$_POST['email'];
$register=$_GET['register'];
$con=mysql_connect('localhost','root','root') or die ('Error connecting to mysql');
mysql_select_db('mailinglist');
$query="INSERT INTO mailinglist (id,firstname,lastname,city,state,email) VALUES (NULL , '$firstname', '$lastname', '$city', '$state', '$email')";
mysql_query($query) or die('Error, insert query failed');
mysql_close($con);
if ($register=='yes') echo "Thank you for registering $firstname $lastname!"
?>
Here is what the reslting table looks like in MySQL... all fields are empty except for "id" which cant be null.
