Jump to content

Having an Undefined index problem.


iamLearning
Go to solution Solved by 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.");
	}

}
 ?>
Edited by iamLearning
Link to comment
Share on other sites

  • Solution

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.");
	}

}
 ?>
Edited by iamLearning
Link to comment
Share on other sites

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)) ) {
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.