Jump to content

undefined index error/warning


katiefox

Recommended Posts

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'])    :confused:     

{

                //get data

  $username = $_POST['username'];

  $password = $_POST['password'];

  $rememberme = $_POST['rememberme'];

}

 

                //check username and password

 

if($username&&$password)    :confused:

{

  //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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.