我的注册表单中输出错误的错误

时间:2015-06-22 13:40:35

标签: php

我正在尝试编写注册表单并在运行SQL查询之前进行一些检查,但是当我测试并尝试生成多个错误时,我只得到第一个错误,或者有时没有错误。我无法找到我犯错误的地方。 以下是PHP中的代码。

//function to filter only phone numbers
function get_phone($number) {
    return preg_replace('#[^0-9]#', '', $number);
}

//function to take only alphabets.
function get_alpha($alphabets){
    return preg_replace('#[^a-z]#', '', $alphabets);
}

//function to check email.
function isValidEmail($email){
    if (strlen ($email) > 50){
        $errors[] = 'email address too long, please use a shorter email address..!';
    } else {
      return (filter_var($email, FILTER_VALIDATE_EMAIL));
       }
    }


function output_errors($errors){
    $output = array();
    foreach($errors as $error) {
        $output[] = '<li>' . $error . '</li>';
    }
    return '<ul>' . implode('', $output) . '</ul>';

    }

if (empty($_POST) === false) {
    //store the text box field names of the form to local variables.
    $cust_name = $_POST['name1'];
    $cust_email = $_POST['email'];
    $cust_phone = $_POST['phone'];
    $cust_addr1 = $_POST['addr1'];
    $cust_addr2 = $_POST['addr2'];
    $cust_city = $_POST['city'];
    $cust_state = $_POST['state'];
    $cust_country = $_POST['country'];
    $username = $_POST['uname'];
    $password = $_POST['passwd'];
    $cnf_passwd = $_POST['cnf_passwd'];
    $sec_que = $_POST['sec_que'];
    $sec_ans = $_POST['sec_ans'];

    //sanitize the inputs from the users end. 
    $cust_name = sanitize($username);
    $cust_phone = get_phone($cust_phone);
    $cust_addr1 = sanitize($cust_addr1);
    $cust_addr2 = sanitize($cust_addr2);
    $cust_city = get_alpha($cust_city);
    $cust_state = get_alpha($cust_state);
    $cust_country = get_alpha($cust_country);
    $username = sanitize($username);  
    $password = md5($password);
    $cnf_passwd = md5($cnf_passwd);
    $sec_que = sanitize($sec_que); //put up dropdown menu
    $sec_ans = sanitize($sec_ans);
    $cust_email = isValidEmail($cust_email);

    //check for error handling in form data
    //1. check for empty fields,
    if ($cust_name == "" || $cust_phone == "" ||
        $cust_addr1 == "" || $username == "" || 
        $password == "" || $cnf_passwd == "" || 
        $sec_que == "" || $sec_ans == ""
       ) {
        $errors[] = 'No blank fields allowed, please fill out all the required fields..!';
        //2.check for field lengths
    } else if (strlen($cust_name) < 3 || strlen($cust_name > 20)) {
        $errors[] = 'The name length should be between 3 to 20, please check & correct..!';
        //3. check for phone number length
    } else if (strlen($cust_phone) < 10 || strlen($cust_phone) > 11) {
        $errors[] = 'The phone number must be 10 or  11 digits..!';
        //4. check for address input lengths.
    } else if (strlen($cust_addr1) < 5 || strlen($cust_addr1) > 50) {
        $errors[] = 'Please provide a valid address..to serve you better..!';
        //5. check if the password fields content match. 
        //length is not checked because the entered values will be converted to MD5 hash 
        // of 32 characters.
    } else if ($password != $cnf_passwd) {
        $errors[] = 'The passwords do not match. Please enter your passwords again..!';
       // 6. check for length of the security answers.  
    } else if (strlen($sec_ans) < 5 || strlen($sec_ans) > 50) {
        $errors[] = 'Please enter a proper security answer..!';
    } //7. check for valid email address
    else if($cust_email == false){
        $errors[] = 'The email address you entered is not valid, please check and correct..!';

    } else {
        execute the SQL queries and enter the values in the database.

        echo 'GOOD...TILL NOW..!!!';
    }

} else {
    $errors [] = 'No data received, Please try again..!!';
}
if(empty($errors) === false) {
?>
<h2>The Following errors were encountered:</h2>
<?php
    echo output_errors($errors); //output the errors in an ordered way.
}
?>

1 个答案:

答案 0 :(得分:0)

使用此结构时:

if () {

} else if () {

} else if () {

}
// etc.

然后只能满足一个条件。只要其中一个if条件为真,其余else if块和最终else块都将被忽略。

如果您的条件不相互排斥,请将它们放在各自独立的区块中:

if () {

}
if () {

}
if () {

}
// etc.