Click here to Skip to main content
15,904,155 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Always having
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\REVIEW\index.php on line 16



This is my code.

<?php
$servername = "localhost";
$username = "";
$password = "";
$remarks= "";
$con = mysqli_connect('localhost', 'root', '', 'firstdb');
if (!$con) {
  die("Connection failed: " . mysqli_connect_error());
}
if(!(empty($_POST["uname"]) && empty($_POST["pin"])))
{
    $UN=$_POST["uname"];
    $PW=$_POST["pin"];
    $sql = "SELECT * FROM users WHERE username=''".$UN." AND password=''".$PW. ";";
	$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
     header( "Location: Main.php" );
     }
   $remarks ="Sorry. Unrecognized username or password.Please try again";
 }
?>

<html>
  <body>
    <form action="index.php" method="post">
      <fieldset>
        <legend>Registration Details:</legend>
        <label for="uname">Username:</label><br>
        <input type="text" id="uname" name="uname" ><br>
        <label for="pin">PIN Code:</label><br>
        <input type="password" id="pin" name="pin" <br><br>
        <input type="submit" value="Register"> <br> 
       <label ><?php echo $remarks; ?></label><br></fieldset>
    </form> 
  </body>
</html> 


What I have tried:

I tried to change some of it even check my semicolons and parenthesis, Im a beginner so it took me so long to fix.
Posted
Updated 9-Nov-20 23:11pm
Comments
Richard Deeming 10-Nov-20 5:07am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]
Richard Deeming 10-Nov-20 5:08am    
You are storing passwords in plain text. Don't do that.
Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

(And no, calling it a "pin" instead of a "password" doesn't change that.)

PHP even has built-in functions to help you do the right thing:
PHP: password_hash[^]
PHP: password_verify[^]

Quote:
PHP
$sql = "SELECT * FROM users WHERE username=''".$UN." AND password=''".$PW. ";";
You have not built your query correctly. You will get a MySQL syntax error when you try to execute this query.

Using a properly parameterized query will fix this, and fix your SQL Injection[^] vulnerability:
PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

Now you just need to fix your insecure password storage.
PHP: password_hash[^]
PHP: password_verify[^]
 
Share this answer
 

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