PHP – How can i show notification bar(Error) when user added wrong username and password

I want to show a notification bar which will display error when user added wrong username and password

I am a beginner in PHP.

I am trying this code and i think there could be a if condition that will be used but didn’t know how to write it.

the database is connected and is working fine. when entered the write username and password it works

<?php

session_start();

include '_database.php';

 $alertShow = false;


if($_SERVER["REQUEST_METHOD"] == 'POST'){

$user = $_POST[ 'user'];
$password = $_POST['pass'];

$user = stripcslashes($user);
$password = stripcslashes($password);

$user = mysqli_real_escape_string($conn , $user);

$password = mysqli_real_escape_string($conn , $password);


$sql = "select * from signin where user_name = '$user' and password = '$password'";  


$result = mysqli_query($conn , $sql);

$row = mysqli_fetch_array($result , MYSQLI_ASSOC);

$count = mysqli_num_rows($result);

if($count==1 ){
    
    session_start();
    $alertShow == false;
    header("Location: index.php?$user");
    
    exit();

}else{
    
    header("Location: login.php?$user");

   
    
}

}






if($alertShow==true){

    echo $alert = '<div class="alert alert-primary" role="alert">
     This is a primary alert—check it out!
     </div>';
 
 }
 

?>

>Solution :

    <?php
session_start();

include '_database.php';

if($_SERVER["REQUEST_METHOD"] == 'POST'){

$user = $_POST[ 'user'];
$password = $_POST['pass'];

$user = stripcslashes($user);
$password = stripcslashes($password);

$user = mysqli_real_escape_string($conn , $user);

$password = mysqli_real_escape_string($conn , $password);


$sql = "select * from signin where user_name = '$user' and password = '$password'";  


$result = mysqli_query($conn , $sql);

$row = mysqli_fetch_array($result , MYSQLI_ASSOC);

$count = mysqli_num_rows($result);

if($count==1 ){
    
    header("Location: index.php?$user");
    
    exit();

}else{
    
    echo '<div class="alert alert-primary" role="alert">
     This is a primary alert—check it out!
     </div>';   
    
}

}
?>

Leave a Reply