Cant Validate forms and add them to database?

Cant Validate forms and add them to database?
0.0 0

#1
         <form action="includes/registration.php" method="POST">
                      <div class="form-group">
                        <input type="text" class="form-control" name="name"  placeholder="Full Name" >
                        </div>
                       
                      
                        <div class="form-group">
                        <input type="text" name="email" placeholder="Email Address" class="form-control">
                     
                      </div>
                      <div class="form-group">
                        <input type="text" name="user" placeholder="pick a username" class="form-control">
                     
                      </div>
                     
                      <div class="form-group">
                        <input type="password" name="pass" placeholder="New Password" class="form-control">
                      </div>

                      <br>
                      <span class="small">By clicking Sign Up, you agree to our <a href="#">Terms</a>,<a href="#"> Data Policy</a> and <a href="#">Cookie Policy</a> . You may receive SMS notifications from us and can opt out at any time</span>
                      <br>
                      <br>
                      <center>
                      <button type="submit" class="btn btn-success">Sign UP</button> 
                        </center>
                  </form>

the above one is the form and the below i added registration.php to validate the inputs and them to the database but it only runs the else statement so, please help me to find my mistake

<?php  

if(isset($_POST['submit'])){
    
    include_once 'db.php';

    $name = mysqli_real_escape_string($conn,$_POST['name']);
    $email = mysqli_real_escape_string($conn,$_POST['email']);
    $user = mysqli_real_escape_string($conn,$_POST['user']);
    $pass = mysqli_real_escape_string($conn,$_POST['pass']);
//Validation and Sanitization goes here
    if(empty($name) || empty($email) || empty($user) || empty($pass) ){
        header("Location: ../index.php?SignUp=blankValues");
        exit();
    }else{
        if(!(preg_match("/^[a-zA-Z ]*$/",$name))){
            header("Location: ../index.php?SignUp=nameOnlyContainWords&Space");
            exit();
        }else{
            if(!(filter_var($email,FILTER_VALIDATE_EMAIL))){
                header("Location: ../index.php?SignUp=invalidEmailEntry");
            exit();
            }else{
                $sql = "SELECT * FROM registration WHERE puser = '$user';";
                $result = mysqli_query($conn,$sql);
                $resCheck = mysqli_num_rows($result);
                if($resCheck > 0){
                    header("Location: ../index.php?SignUp=USERTAKEN");
            exit(); 
                }else{
                    $hashed = password_hash($pass,PASSWORD_DEFAULT);
                    //insert database here
                    $sql = "INSERT INTO registration (pname,pemail,puser,pwd) VALUES ($name,$email,$user,$hashed);";
                    mysqli_query($conn,$sql);
                    header("Location: ../index.php?SignUp=SUCCESS");
            exit();

                }
            }
        }
    }
}else{
    header("Location: ../index.php?SignUp=invalidEntry");
    exit();
}
?>

#2

Which else statement? You have many. Also, can you explain in detail exactly what you are expecting the php code to do? Can you provide us the inputs you are trying for the form and what result you are getting versus what result you expect to get?


#3

It runs the last else statement that is index.php?signup=invalidEntry everytime i click the signup button
As it has to validate the input fields and when all data input are correct then it will add the data to database and show me message in url index.php?signup=success but it beither validates proceed just show me index.php?signup=invalidEntry