My form still gives me a "Registration Successful!" and stores the data in mysql even if it is not a valid data and empty. Then after showing the "Registration Successful" it displays the errors.
The way it should run is, when the input is not valid or empty it will not store in the database and won't alert "Registration Successful!". I put the php codes in the same file as the html, to be specific at the top of the html code.
php
<?php
mysql_connect("localhost","root","");
mysql_select_db("registration");
if (isset($_POST['register'])){
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email'];
$pword = $_POST['password_verify'];
if($_POST){
$errors = array();
if(empty($_POST['first_name'])){
$errors['first_name1'] = "This field cannot be empty";
}
if (empty($_POST['last_name'])){
$errors['last_name1'] = "Please enter your name last name.";
}
if (empty($_POST['email'])){
$errors['email1'] = "Please enter email address.";
}
if (empty($_POST['password'])){
$errors['password1'] = "Please enter password.";
}
if (empty($_POST['password_verify'])){
$errors['password_verify1'] = "Please enter password.";
}
if (strlen($_POST['password']) < 6){
$errors['password2'] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] != $_POST['password_verify']){
$errors['password_verify2'] = 'Your passwords do not match.';
}
$check_email = "select * from customer_info where Email='$email'";
$run = mysql_query($check_email);
if (mysql_num_rows($run) >0){
$errors['email2'] = 'Email already exists.';
}
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";
if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
}
}
?>
You should put condition before this statement
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";
It should be:
if(count($errors)>0){
echo "<script>alert('Error!')</script>";
} else {
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";
if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
}
}
Try This:
if(!isset($error)){
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";
if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
Anddd someone beats me to the punch >.< xD I would also recommend avoid using mysql_* functions as they are deprecated. Try using mysqli_* or Prepared Statements:
http://php.net/manual/en/pdo.prepared-statements.php
http://www.w3schools.com/php/php_mysql_prepared_statements.asp
Before inserting data into database. Please check whether $error
array is empty. If array is empty it means your data is valid else invalid.
if(empty($errors))
{
// insert into database and alert('Registration successful');
}
else
{
// alert('Error');
}