joebegborg07
joebegborg07

Reputation: 839

PHP variable not set

I'm still new at php and trying to learn. I have the following code and the variables $username and password are not being set, and I'm 99% sure that nothing is wrong with them.

Can you guide me in what I can do to solve this. Everytime I run the script I get a username and password not set message (set with if statement).

    <?php
        require 'connect.inc.php';

        if (isset($_POST['login_button'])&&($_POST['username'])&&($_POST['password'])){
                        $login_button = $_POST['login_button'];             
                        $username = $_POST['username'];
                        $password = $_POST['password'];

                            $password_hash = md5($password);

                                if(!empty($username)&&!empty($password)){
                                    $sql = "SELECT `id` FROM `golden_acres_username` WHERE `uname`='$username' AND `password`='$password_hash'";
                                        if($sql_run = mysql_query($sql)){
                                            $query_num_rows = mysql_num_rows($sql_run);
                                        }
                                            if($query_num_rows==0){
                                                echo mysql_result($sql_run);
                                            }
                                            else if($query_num_rows==1){
                                                echo 'ok';
                                            }
                                } else {
                                echo 'You must supply a username and a password.';
                                }
                            }

                     else {
                        echo $array;
                        echo 'Username and password are not set';
                    }
                    ?>
                        <form class="home_logon_area" action="" method="POST">
                        <table border="0">
                            <tr><td colspan="2">Username: </td></tr>
                            <tr><td colspan="2"><input type="text" class="text_field" name="username" size="30"/></td></tr>
                            <tr><td colspan="2">Password: </td></tr>
                            <tr><td colspan="2"><input type="password" class="text_field " type="password" name="password" size="30"/></td></tr>
                            <tr><td valign="top"><a class="home_content_link_form" href="no_password.html"> Forgor Password </a></td>
                                <td align="right" rowspan="2"><input type="submit" name="login_button" id="login_button" value="Login"/></td></tr>
                            <tr><td><a class="home_content_link_form" href="register.php"> Register </a></td></tr>
                        </table>
                        </form>
?>

Thanks in advance,

Joseph

Upvotes: 0

Views: 107

Answers (1)

Rich Bradshaw
Rich Bradshaw

Reputation: 72975

Change:

if (isset($_POST['login_button'])&&($_POST['username'])&&($_POST['password'])){

to:

if (isset($_POST['login_button']) && isset($_POST['username']) && isset($_POST['password'])){

Upvotes: 2

Related Questions