Just page refresh adds a line in PHPMyAdmin

If this has been covered, I can’t find it. The line that is added has an “id” and is blank except for an encrypted password (same one every time). When I fill out my registration form and submit, it all works and I can log in. The only problem is that it adds the extra line. Driving me crazy! I’m a complete PHP novice. The only reason I got this far with it is a tutorial I found online by phpacademy. It was made in 2009 and I understand, now, that PHP has changed but I don’t really want to start over since I am this far. If someone could help me figure this out, I would be very grateful. Thanks in advance. :slight_smile: Here is my PHP code (leaving off the page html)

[php]<?php

$submit = $_POST[‘submit’];

if ($submit) {
//form data
$selectTitle = strip_tags($POST [‘selectTitle’]);
$fullname = strip_tags($_POST[‘fullname’]);
$email = strip_tags($_POST[‘email’]);
$address = strip_tags($_POST[‘address’]);
$city = strip_tags($_POST[‘city’]);
$selectState = strip_tags($_POST[‘selectState’]);
$zipcode = strip_tags($_POST[‘zipcode’]);
$selectAge = strip_tags($_POST[‘selectAge’]);
$username = strtolower(strip_tags($_POST[‘username’]));
$password = strip_tags($_POST[‘password’]);

}

// All errors in one array please
$formErrors = array();

if (empty ($_POST[“selectTitle”])){

$formErrors[] = "Title: Must select one.";
	
	$options = array("mr", "mrs", "ms", "miss", "dr",);
foreach ($options as $option) {
    echo '<option value="' . $option . '"';
    if (in_array($variable,$options)) {
        echo " selected";
    }
}

//End of selectTitle here.
}else {
$selectTitle = $_POST[“selectTitle”];

}

if (empty($_POST[“fullname”])) {
$formErrors[] = “Fullname: Missing”;
}
else {
$fullname = $_POST[“fullname”];

}

if (empty($_POST[“email”])) {
$formErrors[] = “Email: Missing”;
}
else {
$email = $_POST[“email”];

}

if (empty($_POST[“address”])) {
$formErrors[] = “Address: Missing”;
}
else {
$address = $_POST[“address”];
}

if (empty($_POST[“city”])) {
$formErrors[] = “City: Missing”;
}
else {
$city = $_POST[“city”];
}

//Beginning selectState field.

if ( empty($_POST[“selectState”]) ) {
$formErrors[] = “State: Must select one!”;
//Ending selectState field.
}

// Define state here. Use them in HTML
$stateOptions = array(‘AL’=>“Alabama”,
‘AK’=>“Alaska”,
‘AZ’=>“Arizona”,
‘AR’=>“Arkansas”,
‘CA’=>“California”,
‘CO’=>“Colorado”,
‘CT’=>“Connecticut”,
‘DE’=>“Delaware”,
‘DC’=>“District Of Columbia”,
‘FL’=>“Florida”,
‘GA’=>“Georgia”,
‘HI’=>“Hawaii”,
‘ID’=>“Idaho”,
‘IL’=>“Illinois”,
‘IN’=>“Indiana”,
‘IA’=>“Iowa”,
‘KS’=>“Kansas”,
‘KY’=>“Kentucky”,
‘LA’=>“Louisiana”,
‘ME’=>“Maine”,
‘MD’=>“Maryland”,
‘MA’=>“Massachusetts”,
‘MI’=>“Michigan”,
‘MN’=>“Minnesota”,
‘MS’=>“Mississippi”,
‘MO’=>“Missouri”,
‘MT’=>“Montana”,
‘NE’=>“Nebraska”,
‘NV’=>“Nevada”,
‘NH’=>“New Hampshire”,
‘NJ’=>“New Jersey”,
‘NM’=>“New Mexico”,
‘NY’=>“New York”,
‘NC’=>“North Carolina”,
‘ND’=>“North Dakota”,
‘OH’=>“Ohio”,
‘OK’=>“Oklahoma”,
‘OR’=>“Oregon”,
‘PA’=>“Pennsylvania”,
‘RI’=>“Rhode Island”,
‘SC’=>“South Carolina”,
‘SD’=>“South Dakota”,
‘TN’=>“Tennessee”,
‘TX’=>“Texas”,
‘UT’=>“Utah”,
‘VT’=>“Vermont”,
‘VA’=>“Virginia”,
‘WA’=>“Washington”,
‘WV’=>“West Virginia”,
‘WI’=>“Wisconsin”,
‘WY’=>“Wyoming”);

if (empty($_POST[“zipcode”])) {
$formErrors[] = “Zip: Missing”;
}
else {
$zipcode = $_POST[“zipcode”];
}

if (empty ($_POST[“selectAge”])){
$formErrors[] = “Age: Must select one.”;

	$options = array("10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70-79", "80-89", "90-100", "100+");

foreach ($options as $option) {
echo ‘<option value="’ . $option . ‘"’;
if (in_array($variable,$options)) {
echo " selected";
}

}

}

if (empty($_POST[“username”])) {
$formErrors[] = “Username: Missing”;
}
else {
$username = $_POST[“username”];
}

if (empty($_POST[“password”])) {
$formErrors[] = “Password: Missing”;
}
else {
$password = $_POST[“password”];
}

if (empty($_POST[“repeatpassword”])) {
$formErrors[] = “Repeat Password: Missing”;
}
else {
$repeatpassword = $_POST[“repeatpassword”];
}

if ($password==$repeatpassword)
{
}
else
$formErrors[] = “Password: Your passwords do not match.”;

//check char length of username
if (strlen ($username)>15) {
$formErrors[] = “Username: Length of username is too long.”;
}

if (strlen ($_POST[“password”])>15||strlen($_POST[“password”])<8)
{
$formErrors[] = “Password: Must be between 8 and 15 characters”;
}

//encrpt password
$password = md5($password);
$repeatpassword = md5($repeatpassword);

//open database
$connect = mysql_connect(“xxxx”,“xxxx”,“xxxx”);
mysql_select_db(“booksaready”);//select database

$namecheck = mysql_query ("SELECT username FROM users WHERE username='$username'");
$count = mysql_num_rows ($namecheck);

	if ($count!=0)	
	{
					
	$formErrors[] = "That username already exists.";

}

$queryreg = mysql_query("INSERT INTO users VALUES ('','$username','$password','$selectTitle','$fullname','$email','$address','$city','$selectState','$zipcode','$selectAge')");

// Successful registration
if( $submit && empty($formErrors) ) {
die(“You are now registered! Return to login page.”);
}

?>[/php]

I took out the sql server info, don’t post that info - you’re just asking for trouble.

Is it inserting 2 rows?

this[php]
if( $submit && empty($formErrors) ) {
die(“You are now registered! Return to login page.”);
}[/php]
is actually kinda backwards coding. You need to make sure there’s no errors before you do the insert, not afterwards and you’ve already detected the submit press because the script is running.

change [php]if ($password==$repeatpassword)
{
}
else

$formErrors[] = “Password: Your passwords do not match.”;[/php]
to [php]

if ($password != $repeatpassword) {
$formErrors[] = “Password: Your passwords do not match.”;
} else if(strlen ($_POST[“password”])>15||strlen($_POST[“password”])<8) {
$formErrors[] = “Password: Must be between 8 and 15 characters”;
} else {
$password = md5($password);
$repeatpassword = md5($repeatpassword);
}[/php]

Change the insert portion to
[php]if(!$formErrors) {
$queryreg = mysql_query(“INSERT INTO users VALUES (’’,’$username’,’$password’,’$selectTitle’,’$fullname’,’$email’,’$address’,’$city’,’$selectState’,’$zipcode’,’$selectAge’)”) or die(mysql_error());

if($queryreg) {
    // Successful registration
    die("You are now registered! <a href='index.php'>Return to login page</a>.");
}

}[/php]

Here’s a nice RegEx way of checking a password and in my opinion I wouldn’t limit the length of a password for majority of people don’t bother writing really long passwords to start with.
[php] if (preg_match("/^.(?=.{8,})(?=.[0-9])(?=.[a-z])(?=.[A-Z]).*$/", $password) === 0) {
$formErrors[] = ‘Password must be at least 8 characters, and must contain at least one lower case letter, one upper case letter and one digit’;
}[/php]

Thanks “richei” for looking out for me AND fixing my problem!!!..and you too “Strider64” for your suggestion which makes perfect sense. I was able to implement it without incident…miracle. lol. You guys should check out being a “Wizpert” if you haven’t already. 8)

Sponsor our Newsletter | Privacy Policy | Terms of Service