Before telling me in the comments that there are other multiple articles that might help me ant link me to them, I have read most of the questions posted on this website and others and none of their solutions worked for me. I'm building a website (school work so there are no safety concerns) and it's giving me the following error:
Notice: Undefined index: myusername in C:\xampp\htdocs\Anime_Heaven\login.php on line 28.
The context of the page is, as you might have noticed, a login form.
My code is the following:
<?PHP
$db = mysqli_connect('localhost','root','');
if (!$db)
{
echo '<p> Erro: Falha na Ligação';
exit;
}
mysqli_select_db($db,'Anime Heaven');
if($_SERVER["REQUEST_METHOD"] == "POST") {
$myusername = $_POST['user'];
$mypassword = $_POST['pass'];
$sql = ("SELECT id FROM login WHERE user = '$myusername' and pass = '$mypassword';");
$result = mysqli_query($db,$sql) or die(mysqli_error($db));
$row = mysqli_num_rows($result);
if($row == 1) {
$registo = mysqli_fetch_array($result);
$_SESSION['myusername'] = $registo['myusername'];
require_once("main.html");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
When I input the right username and password, it does redirect me to the correct page but it shows the error the way it's shown below:
I would be very glad if someone could help me. Thank you for reading.
CodePudding user response:
Your error message does not really match your code. Message says "index: username" but you are checking for $registo['myusername']. Anyway, in your query you do only a select for the id! Your $registo can't have a "myusername". Select all the fields you need and also check afterwards if the array has that key before assigning it to the session.
CodePudding user response:
There are two methods can be used:
- use
isset()
to check if such index exists:
if (isset($registo['myusername']))
{
$_SESSION['myusername'] = $registo['myusername'];
}
- use
@
to suppress any warning.
$_SESSION['myusername'] = @$registo['myusername'];