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");
?>

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.