Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
PHP
<?php
include "test.php";
 ?>
 
<html>

<head>
    <title>Login </title>
</head>

<body>
	<form name="login_form" class="form-vertical" action="" method="post">
		<h3>Login Page</h3>
		Username: <input type="text" name="username" required/>
		Password: <input type="password"  name="password" required/>
		<center>
            <input type="submit" name="submit_1" value="Login" />
        </center>
	 </form>
	<?php
		if (isset($_POST["submit_1"]))
		{
			$username=$_POST["username"];
			$password=$_POST["password"];
			$count=0;
			$sql="select * from employee where username='$username'&& password='$password'";
			$res=mysqli_query($conn,$sql);
			$count=mysqli_num_rows($res);
			if($count>0&&!$res)
			{
			 header("Location:index.php");
			}
			else
            {
        ?>
		<div>Invalid Username or Password</div>
    <?php
            }

	}
    ?>
</body>

</html>


What I have tried:

It always shows "Invalid Username or Password".
Posted
Updated 28-Sep-20 2:14am
v3
Comments
Richard Deeming 28-Sep-20 8:34am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
Richard Deeming 28-Sep-20 8:35am    
You're storing passwords in plain text. NEVER do that.
Secure Password Authentication Explained Simply[^]

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]
Richard Deeming 28-Sep-20 8:36am    
MySQL :: MySQL 8.0 Reference Manual :: 12.4.3 Logical Operators[^]
"The && operator is a nonstandard MySQL extension. As of MySQL 8.0.17, this operator is deprecated and support for it will be removed in a future MySQL version."

Most likely because the username and password are not found in the database. And the error message confirms it: see Mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in[^].
 
Share this answer
 
Comments
CPallini 28-Sep-20 8:15am    
5.
As already noted by Richard, your query failed. See, for instance PHP mysqli query() Function[^] (have a look at 'return value' section).
You have to test the $res value, before calling mysqli_num_rows($res), e.g.
PHP
if ( $res )
{
  $count=mysqli_num_rows($res);
  //...
}
else
{
  // handle here the query failure
}
 
Share this answer
 
v2
Comments
Richard MacCutchan 28-Sep-20 9:06am    
5 for you. I wonder how many more times we will need to post this answer?
CPallini 29-Sep-20 2:07am    
:-)
Thank you, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900