Jump to content


Photo

[Error]Undefined Index In Login System


  • Please log in to reply
4 replies to this topic

#1 Diether

Diether

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 25 November 2012 - 11:27 AM

Hi guys, can someone help me to solve my problem in creating a login system? :( thanks in advance


i get this error
Notice: Undefined index: txtUsername in C:\xampp\htdocs\online\storeAdmin\admin_login.php on line 6
Notice: Undefined index: txtPassword in C:\xampp\htdocs\online\storeAdmin\admin_login.php on line 7

This are the two lines :
$userName = $_POST['txtUsername'];
$password = $_POST['txtPassword'];

The code i use:
function doLogin()
{
    // if we found an error save the error message in this variable
    $errorMessage = '';
    $userName = $_POST['txtUsername'];
    $password = $_POST['txtPassword'];
   
    // first, make sure the username & password are not empty
    if ($userName == '') {
	    $errorMessage = 'You must enter your username';
    } else if ($password == '') {
	    $errorMessage = 'You must enter the password';
    } else {
	    // check the database and see if the username and password combo do match
	    $sql = "SELECT user_id
			    FROM tbl_user
			    WHERE user_name = '$userName' AND user_password = PASSWORD('$password')";
	    $result = dbQuery($sql);
   
	    if (dbNumRows($result) == 1) {
		    $row = dbFetchAssoc($result);
		    $_SESSION['plaincart_user_id'] = $row['user_id'];
		   
		    // log the time when the user last login
		    $sql = "UPDATE tbl_user
				    SET user_last_login = NOW()
				    WHERE user_id = '{$row['user_id']}'";
		    dbQuery($sql);
		    // now that the user is verified we move on to the next page
		    // if the user had been in the admin pages before we move to
		    // the last page visited
		    if (isset($_SESSION['login_return_url'])) {
			    header('Location: ' . $_SESSION['login_return_url']);
			    exit;
		    } else {
			    header('Location: index.php');
			    exit;
		    }
	    } else {
		    $errorMessage = 'Wrong username or password';
	    }	   
		   
    }
   
    return $errorMessage;
}
<html>
	  <form id="form1" name="form1" method="post" action="admin_login.php">
		    <p align="center">username:<input type="text" name="txtUsername" id="username" />
		    </p>
		    <p align="center">password:<input type="password" name="txtPassword" id="password" />
		    </p>
		    <p align="center">&nbsp;</p>
		  </blockquote>
		  <p align="center">
		    <input type="submit" name="log_in" id="log_in" value="Log in" />
		  </p>
	    </form>


#2 doddsey_65

doddsey_65

    Advanced Member

  • Members
  • PipPipPip
  • 904 posts
  • LocationNorth East UK
  • Age:26

Posted 25 November 2012 - 11:31 AM

Basically your _POST data does not exist. This script is looking for "txtUsername" in the $_POST array and it isn't there. Are you using the right input name from HTML? Has the form been submitted when this script is ran?

Edited by doddsey_65, 25 November 2012 - 11:31 AM.

Test and help out with a new
100% open source forum package: A Simple Forum
Check the A Simple forum Github Page
Visit my Github profile to see what I work on.

#3 Diether

Diether

    Advanced Member

  • Members
  • PipPipPip
  • 41 posts

Posted 25 November 2012 - 11:41 AM

the form wasn't submitted yet when i encounter the error, :( how can i debug this? any tips?

Edited by Diether, 25 November 2012 - 11:42 AM.


#4 doddsey_65

doddsey_65

    Advanced Member

  • Members
  • PipPipPip
  • 904 posts
  • LocationNorth East UK
  • Age:26

Posted 25 November 2012 - 11:50 AM

Since the form hasn't been submitted when this code is ran $_POST['txtUsername'] won't exist. You can check if it exists before running it with

if (isset($_POST['txtUsername']))

Test and help out with a new
100% open source forum package: A Simple Forum
Check the A Simple forum Github Page
Visit my Github profile to see what I work on.

#5 PFMaBiSmAd

PFMaBiSmAd

    Advanced Member

  • Staff Alumni
  • 16,767 posts
  • LocationColorado, U.S.A.

Posted 25 November 2012 - 12:04 PM

All of your form processing code should be inside of an if(){} conditional statement so that it is only executed if the form has been submitted.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com