if-else语句strlen不工作php

时间:2015-05-08 22:43:56

标签: php mysql if-statement strlen

我正在尝试创建工作注册验证但是我遇到了问题。验证有效,但是存在错误,即使用户输入了正确的信息,也没有执行INSERT语句。但是,当" elseif"语句被删除然后错误在界面上回显,但即使用户表单中有错误,数据仍然是INSERTED;比如密码只有字母。如果有人能解决这个错误,我将不胜感激。完整的验证如下。 谢谢 瓦西姆

<?php include "connection2.php" ?>
<?php
session_start();
if (empty($errors)) { 
    if(isset($_POST['submitted'])) { 

        $firstname=$_POST['Firstname'];
        $lastname=$_POST['Lastname'];
        $username=$_POST['Username'];
        $password= $_POST['Password'];
        $email=$_POST['Email'];


        if ($username&&$password&&$email) {

            if (strlen($username)>10) {
                echo "Username is too long (Max 10 Characters)";
            }
            else {           

                //check password length
                if (strlen($password)>15||strlen($password)<5) {
                    echo "Password must be 5 to 15 characters<br>";
                }
                if (!preg_match("#[0-9]+#", $password)) {
                    echo "Password must include at least one number!<br>";
                }
                if (!preg_match("#[a-zA-Z]+#", $password)) {
                    echo "Password must include at least one letter!<br>";
                }

                elseif ((!strlen($username) >10) and

                        (!strlen($password)>15||!strlen($password)<5) and     
                        (preg_match("#[0-9]+#", $password)) and 
                        (preg_match("#[a-zA-Z]+#", $password))) {


                    $sql = mysql_query
                        ("INSERT INTO users(firstname, lastname,username, password, email)         

            VALUES ('$firstname','$lastname','$username',  

            '$password','$email')");

                    md5($password);
                    //register the user!

                    echo "Your Are Now a Member";




                }

            }
        }

        mysql_close(); 
    }}
else { ?>
    <?php }
?> 

2 个答案:

答案 0 :(得分:1)

您的elseif仅附加到上一个if。而且我不确定你的所有反转逻辑是否正确。但更简单的解决方案是只设置一个变量,指示是否有任何验证检查失败,并对此进行测试。

if ($username && $password && $email) {

    if (strlen($username)>10) {
        echo "Username is too long (Max 10 Characters)";
    }
    else {           
        $error = false;
        //check password length
        if (strlen($password)>15||strlen($password)<5) {
            echo "Password must be 5 to 15 characters<br>";
            $error = true;
        }
        if (!preg_match("#[0-9]+#", $password)) {
            echo "Password must include at least one number!<br>";
            $error = true;
        }
        if (!preg_match("#[a-zA-Z]+#", $password)) {
            echo "Password must include at least one letter!<br>";
            $error = true;
        }

        if (!$error) {
            // insert new users
        }
    }
}

答案 1 :(得分:0)

在PHP中,elseif和'else if'构造可能需要更多大括号,请参阅http://php.net/manual/en/control-structures.elseif.php上的注释

另外检查运算符优先级。使用关键字'和'和'或'与使用'||'具有相同的权重或'&amp;&amp;'。我建议添加额外的括号和括号,以使条件更清晰。