katiefox Posted March 31, 2010 Share Posted March 31, 2010 Hi all this is my first post and im an absolute noob with PHP so be kind! im getting an undefined index error on line 3 for login and an undefined variable: username on line 13 and i cant see why any help would be greatly appreciated. thanks in advance <?php if($_POST['login']) { //get data $username = $_POST['username']; $password = $_POST['password']; $rememberme = $_POST['rememberme']; } //check username and password if($username&&$password) { //set login variable so we can refer back to what the query returns $login = mysql_query("SELECT * FROM lecturer WHERE username='$username'"); //using while loop to make more secure by preventing SQL injection which could be a problem if we chaecked for username AND password while($row = mysql_fetch_assoc($login)) { $user_password = $row['password']; if(($password) == $user_password) $loginsuccess = TRUE; else $loginsuccess = FALSE; //if successful login set cookie or session if($loginsuccess==TRUE) { if($rememberme=="on") setcookie("username", $username, time()+1800); else if($rememberme=="") $_SESSION['username']=$username; header("location: search.php"); exit(); } else die("Incorrect username or password please try again."); } } else { die("Please enter a username and password."); } ?> <br><br><br> <input type="image" src="dculogo.gif"> <br><br><br><br><br> <!-- Create login form Using post to make form more secure --> <form action="login.php" method="post"> Username:<br/> <input type="text" name="username"><p/> Password:<br/> <input type="password" name="password"><p/> <input type="checkbox" name="rememberme">remember me<br/><br/> <input type="submit" name="login" value="Log in" </form> Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/ Share on other sites More sharing options...
beingalex Posted March 31, 2010 Share Posted March 31, 2010 Try $username = NULL; $password = NULL; $rememberme = NULL; Under the opening <?php tag Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034691 Share on other sites More sharing options...
katiefox Posted March 31, 2010 Author Share Posted March 31, 2010 hi beingalex, thanks for your help tried that and it got rid of the undefined variable for username but i am still getting undefined index: login? Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034695 Share on other sites More sharing options...
beingalex Posted March 31, 2010 Share Posted March 31, 2010 try: ini_set("error_reporting", "E_ALL & ~E_NOTICE"); Just under <?php Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034698 Share on other sites More sharing options...
katiefox Posted March 31, 2010 Author Share Posted March 31, 2010 i think that just turns off warnings and errors, which i would prefer to leave on so my code will be good. i did try it and it does stop it but the code does not work it i jumping straight to "Please enter a username and password" at the very end of the if statement. thanks again for your help but this is a college project so the code needs to be correct Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034700 Share on other sites More sharing options...
san4os Posted March 31, 2010 Share Posted March 31, 2010 Hello! try if (isset($_POST['login'])) {...} Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034705 Share on other sites More sharing options...
beingalex Posted March 31, 2010 Share Posted March 31, 2010 Glad you're trying to do it properly. If you change if($_POST['login']) { To: if(isset($_POST['login']) && $_POST['login']) { That should work Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034708 Share on other sites More sharing options...
katiefox Posted March 31, 2010 Author Share Posted March 31, 2010 hi san4os i have put in your code beingalex as it seams more detailed, now th errors are gone but for some reason the code is going straight to the final else die("please enter username and password") instead of giving me the option to enter a username and password! any ideas guys?! thanks again for the speedy answers Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034718 Share on other sites More sharing options...
ukweb Posted March 31, 2010 Share Posted March 31, 2010 just stick an @ infront of any variable without a value :-) Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034724 Share on other sites More sharing options...
katiefox Posted March 31, 2010 Author Share Posted March 31, 2010 thanks for all your help guys i found the problem by removing this line it all displays correctly, it must have just went through the if(statements) and went straight to this! else { die("Please enter a username and password."); } Link to comment https://forums.phpfreaks.com/topic/197111-undefined-index-errorwarning/#findComment-1034752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.