Page 1 of 1

undefined index username

PostPosted: 16. October 2011 12:47
by apolakay
Hi everyone
I'm new for php i watch tutorial user login and log out and i just follow the code but when i try it i have an error like this:Notice: Undefined index: username in C:\xampp\htdocs\xampp\member.php on line 5
and this is my code for login
<?php

session_start();
$username = $_POST['username'];
$password = $_POST['password'];
if ($username&&$password)
{
$connect = mysql_connect("localhost","root","") or die ("Couldn't connect!");
mysql_select_db("phplogin") or die ("Couldn't find DB");
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
$numrows = mysql_numrows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
if ($username==$dbusername&&$password==$dbpassword)
{
echo "your login <a href='member.php'>Click</a> here to enter the member page.";
$_SESSION['username']==$dbusername;
}
else
echo "Incorrect password";
}
else
die("That user doesn't exist");

}
else
die("Please enter a username or Password");

?>

and other code for membership.php
<?php

session_start();

echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a>"; <== THIS IS MY ERROR


?>

Re: undefined index username

PostPosted: 16. October 2011 12:59
by Altrea
Hi apolakay,

before we can help you we need some information about your XAMPP Version (if XAMPP is used) and your Operating System.

P.S.: Your PHP error doesn't match on your information. The error is referencing to member.php, your code where you think the error is is named by you membership.php. What is the truth?

Re: undefined index username

PostPosted: 16. October 2011 13:05
by apolakay
Hi
I used XAMPP Version is 1.7.4 and my Operating system is Windows 7.sorry for the wrong information i used for member.php.

Re: undefined index username

PostPosted: 16. October 2011 13:12
by Altrea
Windows 7 Home Premium / Professional / Ultimate? 32Bit or 64Bit? Be as specific as possible please.
Do you use the new XAMPP control panel v3 or the standard one delivered with XAMPP 1.7.4?

Re: undefined index username

PostPosted: 16. October 2011 13:31
by apolakay
I used window 7 Ultimate 32 bit. and i used XAMPP control panel vesion 2.5

Re: undefined index username

PostPosted: 16. October 2011 13:47
by Altrea
Okay, the good news first:

Your notice don't have any effect on the functionality of your code.
Thats just a message from your PHP Parser that you want to use a variable (or in your case a index of an array) which isn't set at the moment.

In your code thats the point, if you haven't sended your register form.

You could redefine the error_reporting of your PHP to hide this messages, or you could tweak your code a little bit to get rid of them (which i wouldn't recommend if you want to follow a tutorial).

Re: undefined index username

PostPosted: 17. October 2011 08:37
by Nobbie
>$_SESSION['username']==$dbusername;

That is simply wrong, as it is a boolean expression (a comparison), and not an assignment!

You must use '=' instead of '=='.