Why isn’t this regular expression working to check for numbers in an email?

  database, mysql, php

I have this regular expression to check whether the email contains a number or not, and if it does it will post the form to a certain table in my database, and if not then it will post it to the another table. However, it just keeps posting it to the other table no matter what?

$pattern = "/[0-9]/";
if (preg_match($pattern, $email) == 1) {
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST") {

    // Validate username
    if(empty(trim($_POST["username"]))) {
        $username_err = "Please enter a username.";
    } else {
        // Prepare a select statement
        $sql = "SELECT studentUsername FROM students WHERE studentUsername = ?";

        if($stmt = $mysqli->prepare($sql)) {
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("s", $param_username);

            // Set parameters
            $param_username = trim($_POST["username"]);

            // Attempt to execute the prepared statement
            if($stmt->execute()) {
                // store result
                $stmt->store_result();

                if($stmt->num_rows == 1) {
                    $username_err = "This username is already taken.";
                } else{
                    $username = trim($_POST["username"]);
                }
            } else {
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            $stmt->close();
        }
    }

    // Validate password
    if(empty(trim($_POST["password"]))) {
        $password_err = "Please enter a password.";
    } elseif(strlen(trim($_POST["password"])) < 6 ) {
        $password_err = "Password must have atleast 6 characters.";
    } else {
        $password = trim($_POST["password"]);
    }

    // Validate forename
    if(empty(trim($_POST["forename"]))) {
        $forename_err = "Please enter a forename.";
    } else {
        $forename = trim($_POST["forename"]);
    }


    // Validate email
    if(empty(trim($_POST["email"]))) {
        $email_err = "Please enter a email.";
    } else {
        $email = trim($_POST["email"]);
    }

    // Validate surname
    if(empty(trim($_POST["surname"]))) {
        $surname_err = "Please enter a surname.";
    } else {
        $surname = trim($_POST["surname"]);
    }


    // Validate confirm password
    if(empty(trim($_POST["confirm_password"]))) {
        $confirm_password_err = "Please confirm password.";
    } else {
        $confirm_password = trim($_POST["confirm_password"]);
        if(empty($password_err) && ($password != $confirm_password)) {
            $confirm_password_err = "Password did not match.";
        }
    }

    // Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($forename_err) && empty($surname_err) && empty($email_err)) {

        // Prepare an insert statement
        $sql = "INSERT INTO students (studentForename, studentSurname, studentEmail, studentUsername, studentPassword) VALUES (?, ?, ?, ?, ?)";

        if($stmt = $mysqli->prepare($sql)) {
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("sssss", $param_forename, $param_surname, $param_email, $param_username, $param_password);

            // Set parameters
            $param_forename = $forename;
            $param_surname = $surname;
            $param_email = $email;
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

            // Attempt to execute the prepared statement
            if($stmt->execute()) {
                // Redirect to login page
                header("location: simple.html");
            } else {
                echo "Something went wrong. Please try again later.";
            }

            // Close statement
            $stmt->close();
        }
    }

    // Close connection
    $mysqli->close();
}
} else {
    // Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST") {

    // Validate username
    if(empty(trim($_POST["username"]))) {
        $username_err = "Please enter a username.";
    } else {
        // Prepare a select statement
        $sql = "SELECT teacherUsername FROM teacher WHERE teacherUsername = ?";

        if($stmt = $mysqli->prepare($sql)) {
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("s", $param_username);

            // Set parameters
            $param_username = trim($_POST["username"]);

            // Attempt to execute the prepared statement
            if($stmt->execute()) {
                // store result
                $stmt->store_result();

                if($stmt->num_rows == 1) {
                    $username_err = "This username is already taken.";
                } else{
                    $username = trim($_POST["username"]);
                }
            } else {
                echo "Oops! Something went wrong. Please try again later.";
            }

            // Close statement
            $stmt->close();
        }
    }

    // Validate password
    if(empty(trim($_POST["password"]))) {
        $password_err = "Please enter a password.";
    } elseif(strlen(trim($_POST["password"])) < 6 ) {
        $password_err = "Password must have atleast 6 characters.";
    } else {
        $password = trim($_POST["password"]);
    }

    // Validate forename
    if(empty(trim($_POST["forename"]))) {
        $forename_err = "Please enter a forename.";
    } else {
        $forename = trim($_POST["forename"]);
    }


    // Validate email
    if(empty(trim($_POST["email"]))) {
        $email_err = "Please enter a email.";
    } else {
        $email = trim($_POST["email"]);
    }

    // Validate surname
    if(empty(trim($_POST["surname"]))) {
        $surname_err = "Please enter a surname.";
    } else {
        $surname = trim($_POST["surname"]);
    }


    // Validate confirm password
    if(empty(trim($_POST["confirm_password"]))) {
        $confirm_password_err = "Please confirm password.";
    } else {
        $confirm_password = trim($_POST["confirm_password"]);
        if(empty($password_err) && ($password != $confirm_password)) {
            $confirm_password_err = "Password did not match.";
        }
    }

    // Check input errors before inserting in database
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err) && empty($forename_err) && empty($surname_err) && empty($email_err)) {

        // Prepare an insert statement
        $sql = "INSERT INTO teacher (teacherForename, teacherSurname, teacherEmail, teacherUsername, teacherPassword) VALUES (?, ?, ?, ?, ?)";

        if($stmt = $mysqli->prepare($sql)) {
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("sssss", $param_forename, $param_surname, $param_email, $param_username, $param_password);

            // Set parameters
            $param_forename = $forename;
            $param_surname = $surname;
            $param_email = $email;
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash

            // Attempt to execute the prepared statement
            if($stmt->execute()) {
                // Redirect to login page
                header("location: simple.html");
            } else {
                echo "Something went wrong. Please try again later.";
            }

            // Close statement
            $stmt->close();
        }
    }

    // Close connection
    $mysqli->close();
}
}

I am trying to figure out why, I cannot?

Source: Ask PHP

LEAVE A COMMENT