-3

I checked other questions of " Undefined index: username". and none of these questions were properly answered. So I decide to make my own question.

Notice: Undefined index: username in D:\\xampp\htdocs\project\test.php on line 19

Below is the code which shows the above error:

$username = 'username';
$password = 'password';

$sessionQuery = "SELECT id FROM admin WHERE username='$username' AND password='$password'";
$sessionMysqlQuery = mysql_query($sessionQuery);
if(mysql_num_rows($sessionMysqlQuery) === 0){
    header('Location: ./page.php');
}
King
  • 23
  • 8
  • 2
    Show line `19` please. – Shoe Oct 25 '14 at 12:34
  • 1
    You should have something like `foo['username']` in your code... – Christian Gollhardt Oct 25 '14 at 12:34
  • What a coincidence that my username is `admin' --` and I can log in as admin without even knowing the password. Thanks for allowing SQL injection and hacking! You also use cleartext passwords and use deprecated DB functions. Please tell me the URL after you've uploaded this to the internet. – h2ooooooo Oct 25 '14 at 12:37
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Oct 26 '14 at 00:25

1 Answers1

0

Although we are giving guesses here since you are probably missing the code which gives the notice, you are probably trying to reference the username in code similar to:

$username = $row['username'];

and since in your SQL query you are only returning the id of the admin by

SELECT id FROM ...

the returned array does not have the username index.

It's either that or although you are getting an empty result, inside the included page:

if(mysql_num_rows($sessionMysqlQuery) === 0){
    header('Location: ./page.php');
}

you are still trying to access the username index of the result set which means you code's logic is broken or you would want to check for === 1 before including whatever is included in page.php.

Kypros
  • 2,997
  • 5
  • 21
  • 27