Diether Posted November 25, 2012 Share Posted November 25, 2012 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"> </p> </blockquote> <p align="center"> <input type="submit" name="log_in" id="log_in" value="Log in" /> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/271146-errorundefined-index-in-login-system/ Share on other sites More sharing options...
doddsey_65 Posted November 25, 2012 Share Posted November 25, 2012 (edited) 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 November 25, 2012 by doddsey_65 Quote Link to comment https://forums.phpfreaks.com/topic/271146-errorundefined-index-in-login-system/#findComment-1394953 Share on other sites More sharing options...
Diether Posted November 25, 2012 Author Share Posted November 25, 2012 (edited) the form wasn't submitted yet when i encounter the error, how can i debug this? any tips? Edited November 25, 2012 by Diether Quote Link to comment https://forums.phpfreaks.com/topic/271146-errorundefined-index-in-login-system/#findComment-1394956 Share on other sites More sharing options...
doddsey_65 Posted November 25, 2012 Share Posted November 25, 2012 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'])) Quote Link to comment https://forums.phpfreaks.com/topic/271146-errorundefined-index-in-login-system/#findComment-1394957 Share on other sites More sharing options...
PFMaBiSmAd Posted November 25, 2012 Share Posted November 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271146-errorundefined-index-in-login-system/#findComment-1394961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.