Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

forms - how to validate one variable either of two variables in php

i have two variables mobile and email now i want to validate both but i want the user to leave blank one of the fields if user does not have one for ex if a user does not want to register with his email then he can go to mobile number for registration and vice versa this is my validation code

<?php

$emailError    = "";
$fullnameError = "";
$usernameError = "";
$passwordError = "";
$mobileerror = "";
$errors        = 0;

if ((isset($_POST['submit']))) {

    $email    = strip_tags($_POST['email']);
    $fullname = strip_tags($_POST['fullname']);
    $username = strip_tags($_POST['username']);
    $password = strip_tags($_POST['password']);
    $mobile = strip_tags($_POST['mobile']);



    $fullname_valid = $email_valid = $mobile_valid = $username_valid = $password_valid = false;



    if (!empty($fullname)) {

        if (strlen($fullname) > 2 && strlen($fullname) <= 30) {
            if (!preg_match('/[^a-zA-Zs]/', $fullname)) {
                $fullname_valid = true;

                # code...
            } else {
                $fullnameError = "fullname can contain only alphabets <br>";
                $errors++;
            }
        } else {
            $fullnameError = "fullname must be 2 to 30 char long <br>";
            $errors++;
        }
    } else {
        $fullnameError = "fullname can not be blank <br>";
        $errors++;
    }



        if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

            $query2 = "SELECT email FROM users WHERE email = '$email'";
            $fire2 = mysqli_query($con, $query2) or die("can not fire  query" . mysqli_error($con));
            if (mysqli_num_rows($fire2) > 0) {
                $emailError = $email . "is already taken please try another one<br> ";
            } else {
                $email_valid = true;
            }
            # code...
        } else {
            $emailError = $email . "is an invalid email address <br> ";
            $errors++;
        }
        # code...

   if ($mobile) {
      $query4 = "SELECT mobile FROM users WHERE mobile = '$mobile'";
            $fire4 = mysqli_query($con, $query4) or die("can not fire  query" . mysqli_error($con));
            if (mysqli_num_rows($fire4) > 0) {
                $mobileerror =  "is already taken please try another one<br> ";
            } else {
                $mobile_valid = true;
            }
   }

    if (!empty($username)) {

        if (strlen($username) > 4 && strlen($username) <= 15) {
            if (!preg_match('/[^a-zA-Zd_.]/', $username)) {


                $query = "SELECT username FROM users WHERE username = '$username'";
                $fire = mysqli_query($con, $query) or die("can not fire  query" . mysqli_error($con));


                if (mysqli_num_rows($fire) > 0) {
                    $usernameError = '<p style="color:#cc0000;">username already taken</p>';
                    $errors++;
                } else {
                    $username_valid = true;
                }

            } else {
                $usernameError = "username can contain only alphabets <br>";
                $errors++;
            }
        } else {
            $usernameError = "username must be 4 to 15 char long <br>";
            $errors++;
        }
    } else {
        $usernameError = "username can not be blank <br>";
        $errors++;
    }

    if (!empty($password)) {
        if (strlen($password) >= 5 && strlen($password) <= 15) {
            $password_valid = true;
            $password       = md5($password);

            # code...
        } else {
            $passwordError = $password . "password must be between 5 to 15 character long<br>";
            $errors++;
        }
        # code...
    } else {
        $passwordError = "password can not be blank <br>";
        $errors++;
    }

    //if there's no errors insert into database
    if ($errors <= 0) {
        if ($fullname_valid && ($email_valid || $mobile_valid )&& $password_valid && $username_valid) {
            $query = "INSERT INTO users(fullname,email,username,password,avatar_path) VALUES('$fullname','$email','$username','$password','avatar.jpg')";
            $fire = mysqli_query($con, $query) or die("can not insert data into database" . mysqli_error($con));
            if ($fire) {


                header("Location: dashboard.php");
            }
        }

    }
}
?>

now when i use email and leave blank mobile the code works fine but when i use email and leave blank mobile then error occurs how to solve this problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use one more flag $isValid_email_mobile = FALSE;

When control flow enters into if (filter_var($email, FILTER_VALIDATE_EMAIL)) then on SUCCESS just set $isValid_email_mobile = TRUE; It will be same if control enters in condition if ($mobile) again on SUCCESS , set it as $isValid_email_mobile = TRUE;

When $isValid_email_mobile = FALSE; becomes TRUE then you know that of the field/variable has passed your requirement and its ready for DB INSERT

Then

In your last IF condition when you try to INSERT just change IF condition to the following

IF ($fullname_valid &&  $isValid_email_mobile && $password_valid && $username_valid)

One more thing whenever you are using Flag logic always set your flag to some default value before using it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...