Jump to content

Notice: Undefined index: username (directory) on line 2.


shortysbest

Recommended Posts

Im building a login forum and i am getting the notices

Notice: Undefined index: username in C:....login.php  on line 2

 

Notice: Undefined index: password in C:\....login.php on line 3

 

I can't figure out what's going on. any help would be thankful :)

<html>
<body>
<form action='login.php' method='POST'>
Username:<input type='text' name='username'><br />
Password:<input type='password' name='password'><br />
<input type='submit' value="Log In">
</form>
</body>
</html>

 

 

<?php
$username = $_POST['username'];   //t his is line 2
$password = $_POST['password'];   // this is line 3

if ($username&&$password)
{
$connect = mysql_connect("localhost","root","") or die ("couldn't connect!");
mysql_select_db("phplogin") or die("couldn't find db");
}

else
die("please enter a username and a password");
?>

 

 

When the page is first requested, the $_POST[] elements do not exist.  They are only there after the user submits the form.  Wrap the login stuff in an if statement:

if(isset($_POST['username']){ ... }

so the code is not executed until the form is submitted.

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.