Search code examples
phpdatabaseauthenticationundefined-index

Why do I get the Undefined Index Error?


I am making a simple login form that saves to a database using MYSQL. I used a tutorial and at the end when I tested it I got an error saying

'Notice: Undefined index: login in C:\wamp\www\database_test\login.php on line 7'

Don't know what this means and after searching the internet still can't figure it out. Here is my code. Please help. Thanks!

 <?php

     session_name('MyLogin');
     session_start();
     session_destroy();

     if ($_GET['login'] == "failed") {    //<------This is line 7//
         print $_GET['cause'];
     }

 ?>
 <html>
     <body>
         <form name='login_form' method='post' action='log.php?action=login'>
             Login: <input type='text' name='user'><br />
             Password: <input type='password' name='pwd'><br />
             <input type='submit'>
         </form>
     </body>
 </html>

I basically copied this from a tutorial at

http://www.howtodothings.com/computers-internet/how-to-make-a-login-system-for-your-website


Solution

  • Dylan, I highly suggest that you RTM. If you don't understand the undefined index error, it's probably not a good idea to start writing code just yet.

    This is thrown on line 7 (like the error states) at $_GET['login']. This error was thrown because login is not a key found in the $_GET array. Therefore, PHP throws the notice. You can add an isset() or !empty() check to avoid the error, like such:

    if (!empty($_GET['login']) && $_GET['login'] == "failed" )