简体   繁体   中英

PHP Registration Script Saying All Fields Are Required when All are filled

The following script does not function as it should. When I go to it on my website and type in all the fields, it still says 'All fields are required'. I modified it a bit from a script on this website: http://www.abell12.com/ . You have to register to be able to download it but once you do, click on Scripts, then PHP, then go to Login And Registration and download the script. Here is my version of the script :

<?php

if(isset($_POST['submit']))
{
    $name = mysql_real_escape_string($_POST['name']);
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']);
    $password1 = mysql_real_escape_string($_POST['password1']);

    $enc_password = md5($password);

    if($name && $username && $password && $password1)
    {
        if(strlen($name)<30)
        {
            if(strlen($username)<10)
            {
                if(strlen($password)<30 || strlen($password)>6)
                {
                    if($password == $password1)
                    {
                        require "dbc.php";
                        $query = mysql_query("INSERT INTO users VALUES ('','$name','$username','$enc_password')");
                        echo "Registration Complete! <a href='loginpage.php'>Click here to login</a>";
                    }
                    else
                    {
                        echo "Passwords must match";
                    }
                }
                else
                {
                    echo "Your password must be between 6 and 30 characters";   
                }
            }
            else
            {
                echo "Your username is too long";   
            }
        }
        else
        {
            echo "Your name is too long";
        }
    }
    else
    { 
        echo "All fields are required"; 
    }
}

?>

<html>

    <form action="register.php" method="POST">
        Name: <input type="text" name="name" value="<?php if(isset($_POST['submit'])){echo "$name";} ?>"> Max Length:30<p>
        Username: <input type="text" name="username" value="<?php if(isset($_POST['submit'])){ echo "$username";} ?>"> Max Length:15<p>
        Password: <input type="password" name="password"> Max length:30<p>
        Re-Enter Password: <input type="password" name="password1"><p>
        <input type="submit" name="submit" value="Register">
    </form>

</html>

If you need more information, feel free to ask me.

There's an error with this line:

if($name && $username && $password && $password1)

You'll need to use empty :

if(empty($name) && empty($username) && empty($password) && empty($password1))

As from the literal meaning empty() means variable is empty. It's a BOOL function, which means that it will return true when empty and false when not empty.

Tip : You should not use MySQL as it is already deprecated, use MySQLi instead.

You use as optimiz code like this.

use below

if($name!='' && $username!='' && $password!='' && $password1!='')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM