Jump to content

Having an Undefined index problem.


iamLearning

Recommended Posts

I am trying to create a small login for the left side page of my website.

 

Although the problem is, I keep receiving the error:

Notice: Undefined index: username in C:\xampp\htdocs\template\leftsidebar.php on line 15

Notice: Undefined index: password in C:\xampp\htdocs\template\leftsidebar.php on line 16

Alright, so I know what it means. It means username and password have not been set yet. I also know that most often people use isset() to check and that's what I did. Please have a look at my code and try to help me with this problem, thanks.

<?php
session_start();

if (isset($_SESSION['username'])){
echo 'Welcome back <font color = "limegreen">'.$_SESSION['username'].'</font>!';
}else{
	echo "
	<form action ='?page=home' method='POST'>
		Username: <input type='text' name='username'><br>
		Password: <input type='password' name='password'><br>
								<input type='submit' value= 'Log In'>
	</form>
	";


$username = $_POST['username'];
$password = $_POST['password'];

if ($username && $password)
{
$connect = mysql_connect("localhost","root","") or die("Couldn't connect to server.");
mysql_select_db("gameplayhr") or die("Couldn't find database");

$query = mysql_query("SELECT * FROM users WHERE username='$username' ");

$numrows = mysql_num_rows($query);

if ($numrows != 0)
//code to login
{
	while ($row = mysql_fetch_assoc($query))
	{
		$dbusername = $row['username'];
		$dbpassword = $row['password'];
	}
//check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
//correct login information
		//You have logged in here.
		$_SESSION['username'] = $username;
}else{
//incorrect login information
		echo "Incorrect password !"; 
		}
}

else{
	die("That username doesn't exist!");
}

}else{
	die("Please enter a username and password.");
	}

}
 ?>
Link to comment
https://forums.phpfreaks.com/topic/277629-having-an-undefined-index-problem/
Share on other sites

EDIT: 

 

Fixed the problem.

 

 

Solution - Although, how would I check for both username and password just to be extra safe?

 

 

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

$username = $_POST['username'];
$password = $_POST['password'];
}

 

<?php
session_start();

if (isset($_SESSION['username'])){
echo 'Welcome back <font color = "limegreen">'.$_SESSION['username'].'</font>!';
}else{
	echo "
	<form action ='?page=home' method='POST'>
		Username: <input type='text' name='username'><br>
		Password: <input type='password' name='password'><br>
								<input type='submit' value= 'Log In'>
	</form>
	";
}
if (isset($_POST['username'])){

$username = $_POST['username'];
$password = $_POST['password'];

if ($username && $password)
{
$connect = mysql_connect("localhost","root","") or die("Couldn't connect to server.");
mysql_select_db("gameplayhr") or die("Couldn't find database");

$query = mysql_query("SELECT * FROM users WHERE username='$username' ");

$numrows = mysql_num_rows($query);

if ($numrows != 0)
//code to login
{
	while ($row = mysql_fetch_assoc($query))
	{
		$dbusername = $row['username'];
		$dbpassword = $row['password'];
	}
//check to see if they match!
if ($username==$dbusername&&$password==$dbpassword)
{
//correct login information
		//You have logged in here.
		$_SESSION['username'] = $username;
}else{
//incorrect login information
		echo "Incorrect password !"; 
		}
}

else{
	die("That username doesn't exist!");
}

}else{
	die("Please enter a username and password.");
	}

}
 ?>

EDIT: 

Solution - Although, how would I check for both username and password just to be extra safe?

I usually do it this way:

$username = (isset($_POST['username']) ? $_POST['username'] : '');
$password = (isset($_POST['password']) ? $_POST['password'] : '');

if ( (!empty($username) and (!empty($password)) ) {

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.