Hi Guys!
So i have a login form thats "supposed" to display an error if a person doesnt enter any information or they enter the wrong information

I have the following code:

<?php
session_start(); // Starting Session
include_once('config.php');

$error=''; // Variable To Store Error Message
if (isset($_POST['submit'])) {
if (empty($_POST['user']) || empty($_POST['pass'])) {
$error = "Username or Password is invalid";
}
else
{
// Define $username and $password
$user=$_POST['user'];
$pass=$_POST['pass'];
// To protect MySQL injection for Security purpose
$user = stripslashes($user);
$pass = stripslashes($pass);
$user = mysqli_real_escape_string($con, $user);
$pass = mysqli_real_escape_string($con, $pass);
// Selecting Database
$db = mysqli_select_db($con, "company");
// SQL query to fetch information of registered users and finds user match.
$query = mysqli_query($con, "SELECT * FROM users where Username='$user' AND Password='$pass'");
$rows = mysqli_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$user; // Initializing Session
header("location: member.php"); // Redirecting To Other Page
} else {
$error = "Username or Password is invalid";
}
mysqli_close($con); // Closing Connection
}
}
?>

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style/style.css"> 
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.min.js"></script>
<title>Login</title>

</head>
<body>

<div id = "logReg">
<span href="#" class="button" id="toggle-login">Log in</span> 
<span href="#" class="button" id="toggle-register">Register</span>
</div>

<div id="login">
  <div id="triangle"></div>
  <h1>Log in</h1>
  <form action = "" method = "POST">
    <input type="text" placeholder="Username" name = "user"/>
    <input type="password" placeholder="Password" name = "pass"/>
    <input type="submit" value="Log in" name = "submit" />

  </form>
</div>

<div id="register">
  <div id="triangle"></div>
  <h1>Register</h1>
  <form>
    <input type="text" placeholder="Username" />
    <input type="text" placeholder="First Name" />
    <input type="text" placeholder="Last Name" />
    <input type="email" placeholder="Email" />
    <input type="password" placeholder="Password" />
    <input type="submit" value="Log in" />
  </form>
</div>

</body>

<script>

$("#toggle-login").click(function(){
    $("#register").hide().attr("formnovalidate");
    $("#login").toggle();
});

$("#toggle-register").click(function(){
    $("#login").hide().attr("formnovalidate");
    $("#register").toggle();
});
</script>
</html>

however if i enter nothing into the two textboxes for the username and password then nothing happens it just stays in the login screen.

Any idea on what ive done wrong?

Thanks guys!

oh btw ignore the register form :P

Recommended Answers

All 3 Replies

but line 7 to 9 has

if (empty($_POST['user']) || empty($_POST['pass'])) {
$error = "Username or Password is invalid";
}

So if the two textboxes are empty then it should display an error?

the posted code never displays the value of $error
eg between 54 and 55

54.5 <?php if(isset($error)) {echo $error;} ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.