I am creating a members only website and am having trouble with the user registration form. I want the registration page to require a minimum of Username, Password, Confirmation Password, and email. If the user does not enter any or all of these fields I would like them to be redirected to a page with a corresponding error and not have their input entered into the database. The problem is that when I test the form with the various fields missing I am redircted to the correct errors pages, but the information still goes to the database. Even if I submit the form completely blank, a blank user account is still created in the database. I've posted the code I'm using below. Any help would be greatly appreciated.
<?php
//connection to database "***"
$host="localhost";
$user="";
$pass="";
$db="***";
$cxn=mysqli_connect($host, $user, $pass, $db);
if (!$cxn)
{
die('Unable to connect to database: ' . mysqli_error($cxn));
}
//registration verification function
$new_user=$POST['username'];
$new_pass=$POST['password'];
$conf_new_pass=$POST['confirm_password'];
$new_lastname=$POST['lastname'];
$new_firstname=$POST['firstname'];
$new_street=$POST['street'];
$new_city=$POST['city'];
$new_state=$POST['state'];
$new_zip=$POST['zip'];
$new_email=$POST['email'];
$new_phone=$POST['phone'];
$new_fax=$POST['fax'];
//form to process user registration
$sql=mysqli_query($cxn,"INSERT INTO members (loginName, password, lastName, firstName)
VALUES ('$new_user','$new_pass', '$new_lastname', '$new_firstname')");
//if (!mysqli_query($sql,$cxn))
//{
//die('Error: ' . mysqli_error($sql));
//}
if (empty($new_user)) { echo "username required"; exit;}
if (empty($new_pass)) { echo "password required"; exit;}
if (empty($new_email)) { echo "email required"; exit;}
if(!preg_match("[a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$", $new_email)) {
echo "Invalid Email"; exit;
}
if ($new_pass != $conf_new_pass) { echo "passwords don't match"; exit;}
else { mysqli_query($sql); }
mysqli_close($cxn)
?>