Page 1 of 1

PHP/MySQL Validation Error

Posted: Thu Jul 17, 2008 4:40 pm
by voidobscura
Hello!

I'm having a problem with this script which allows users to register on a website I am designing. The script accepts a username, password, email address, advertisement type, and advertisement content. I intended to design this script so that upon entering AT LEAST the username,password, and email, the account would be created. However, when I do not enter a password, the account is still created, and the entry shows up in the MySQL database as a single space.

Kindly help me :)

Code: Select all

<?php
    if(isset($_POST['uid']) && isset($_POST['pwd']) && isset($_POST['email']))
    {
        include('connect.php');
        $uid = $_POST['uid'];
        $pwd = $_POST['pwd'];
                $email = $_POST['email'];
        $adtype = $_POST['adtype'];
        $adcontent = $_POST['adcontent'];
        $availability = "SELECT * FROM logins WHERE username='$uid'";
        $result = mysql_query($availability);
        $count = mysql_num_rows($result);
        if($count==0)
        {
            mysql_query("INSERT INTO logins (username,password,adtype,adcontent,email) VALUES ('$uid','$pwd','$adtype','$adcontent','$email')");
            echo "<script>alert('Your account has been created! Thanks!');</script>";
        }
        else
        {
            echo "<script>alert('It seems that your account name is not available.  Please choose a different account name!');</script>";
        }
    }
        
    else
    {
        echo "<form method='post' action='#'>";
        echo "*Username: <input type='text' name='uid' size='16' maxlength='16'/><br>";
        echo "*Password: <input type='password' name='pwd' size='20' maxlength='20'/><br>";
                echo "*E-Mail Address: <input type='text' name='email' size='40' maxlength='40'/><br>";
        echo "Adtype: <input type='text' name='adtype' size='20' maxlength='20'/><br>";
        echo "Ad Content: <input type='textarea' name='adcontent' cols='100' rows='20' maxlength='2000'/><br>";
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='submit' value='Create User!'/>";
        echo "</form>";
    }
?>

Re: PHP/MySQL Validation Error

Posted: Thu Jul 17, 2008 5:23 pm
by Reviresco
Since the "pwd" variable is from a form element, it is still being passed -- it's just blank. You would need to check that it's not empty:

Code: Select all

if (isset($_POST['pwd'] && $_POST['pwd'] != "")

Re: PHP/MySQL Validation Error

Posted: Thu Jul 17, 2008 5:40 pm
by voidobscura
Thanks very much, that worked just fine. Could someone please explain why exactly the isset would still return true although the value of the pwd field is null?

thanks

Re: PHP/MySQL Validation Error

Posted: Thu Jul 17, 2008 5:43 pm
by Benjamin
isset tests if a variable is defined. This included array indexes. empty() would return false if it's null, 0, undefined or false.