[php] $_POST not working. "Notice: Undefined index: username..."

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

So, I am currently learning PHP and was reading on a book about the md5 function for passwords, so I decided to give it a try and see how it goes. I also decided to use the POST method rather than the GET, since I saw people saying that it is safer and doesn't let the variables appearing on the URL.

For my testing project I made a very simple form, which follows:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form action="dologin" method="POST">
            <table border="0">
                <tbody>
                    <tr>
                        <td>Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td>Password:</td>
                        <td><input type="password" name="password"></td>
                    </tr>
                </tbody>
            </table>
            <input type="submit" value="Login">
        </form>
    </body>
</html>

The problem is that when I enter the values on BOTH fields and click "Login" I get the following output on the other PHP file.

Notice: Undefined index: username in C:\xampp\htdocs\md5\home\dologin\index.php on line 11
Username non existent

Here follows the code for the "/dologin/index.php" file

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
            mysql_connect("localhost", "root") or die(mysql_error());
            mysql_select_db("test") or die(mysql_error());
            $query = "SELECT password FROM users WHERE username='".$_POST['username']."'";
            $result = mysql_query($query);
            $row = mysql_num_rows($result);
            if(isset($row)){
                $password = (mysql_result($result,$row - 1));
                $enteredpassword = md5("w@#$@#".$_GET['password']."^#3f%#^");
                if($password == $enteredpassword){
                    if(!isset($_COOKIE['PHPSESSID'])){
                        session_start();
                    }
                    $_SERVER['LOGIN_STATUS'] = true;
                } else {
                    die("Access denied");
                }    
            } else {
                die("Username non existent");
            }

        ?>
    </body>
</html>

Any help at all on my issue is very much appreciated, thank you for reading.

This question is related to php http-post undefined-index

The answer is


first of all,

be sure that there is a post

if(isset($_POST['username'])) { 
    // check if the username has been set
}

second, and most importantly, sanitize the data, meaning that

$query = "SELECT password FROM users WHERE username='".$_POST['username']."'";

is deadly dangerous, instead use

$query = "SELECT password FROM users WHERE username='".mysql_real_escape_string($_POST['username'])."'";

and please research the subject sql injection


You should check if the POST['username'] is defined. Use this above:

$username = "";

if(isset($_POST['username'])){
    $username = $_POST['username'];
}

"SELECT password FROM users WHERE username='".$username."'"