anotsodev
7/26/2016 - 12:59 AM

will show the error if login credential is invalid

will show the error if login credential is invalid

<?php
    include 'dbconfig.php';
    session_start();

    $em_user = $_POST["username"];
    $em_pass = $_POST["password"];
    
    if(!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $statemnt = $conn->prepare('select * from editoraccounts where username = ? and password = ?');

    $statemnt->bind_param('ss',$em_user,$em_pass);

    $statemnt->execute();

    $result = $statemnt->get_result();

    if($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            
            $_SESSION['username'] = $row['username'];
            $_SESSION['first_name'] = $row['first_name'];
            $_SESSION['last_name'] = $row['last_name'];
            header( "Location: loading.php" ); 
        }
    
    }else {
            header( "Location: loading.php" ); //just the loading animation.
            $_SESSION['error'] = '<p class="red-text">Invalid username or password</p>'; //will store the error message to $_SESSION['error'].
            die();
    }
// Check if the $_SESSION['error'] is set.
<?php
  if(isset($_SESSION['error'])){
    echo $_SESSION['error']; 
    unset($_SESSION['error']);;
  } 
?>