PHP/MySQL Validation Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
voidobscura
Forum Newbie
Posts: 2
Joined: Thu Jul 17, 2008 4:29 pm

PHP/MySQL Validation Error

Post 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>";
    }
?>
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: PHP/MySQL Validation Error

Post 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'] != "")
voidobscura
Forum Newbie
Posts: 2
Joined: Thu Jul 17, 2008 4:29 pm

Re: PHP/MySQL Validation Error

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: PHP/MySQL Validation Error

Post 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.
Post Reply