Click here to Skip to main content
14,766,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?php 
   
  $conn = mysqli_connect('localhost','root','','crazysolution');
  if(!$conn) {
    echo'Connection error:'. mysqli_connect_error();
  } 
  $row;
  $error = array('username'=>'','password'=>'');

  if (isset($_POST['login'])) { 

    if(empty($_POST['username'])) {
        $error['username'] = "Enter your name*" ;
    }

    if(empty($_POST['password'])) {
        $error['password'] = "Please enter password";
    }

    if(array_filter($error))
    {
      echo $error;
      header('location : LoginPage.php');
    }
    else
    {
      $username = htmlspecialchars($_POST['username']);
      $password = htmlspecialchars($_POST['password']);
      
      $sql = "SELECT * FROM `register` WHERE username='$username';";

      $result = mysqli_query($conn , $sql) or die(mysqli_error($conn));
      $row = mysqli_fetch_all($result, MYSQLI_ASSOC);
      mysqli_close($conn);
      if(count($row)!==0)
      {
        $databasepassword = $row[0]['password'];
        if($databasepassword === $password) {
          ?>
          <script>
          alert('Login Successful');
          </script>
          <?php
        }
        else {
          ?>
          <script>
          alert('Login Failed');
          </script>
          <?php
        }
        else {
          echo"All fields are required!";
        }
      }
    }
  }
?>  


What I have tried:

I basically tried many things by the help of some websites and videos but still couldn't solved it. So someone help me with this
Posted
Updated 27-Dec-20 1:09am
v2

So little code, so many errors ...

Let's start with the three you haven't noticed, because they are the most important:

1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
DROP TABLE MyTable;
A perfectly valid "delete the table" command
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]3)

And remember: if this is web based then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

3) Never hard-code connection strings: if your code ever gets to production, it will fail immediately, because you connection string refers to a local instance of SQL server, and that won't be the case. Always store connection strings in a configuration file!

Now, the one you have noticed by can't fix yourself.
Look at the error message, it tells you two critical pieces of information: The error message "unexpected 'else'" and where it found it "on line 52".
So look at line 52 (most editors accept CTRL+G as a "go to line number" command):
else {
That matches what the error message says, so look upwards to find it's matching if:
if(count($row)!==0)
{
$databasepassword = $row[0]['password'];
if($databasepassword === $password) {
  ?>
  <script>
  alert('Login Successful');
  </script>
  <?php
}
else {
  ?>
  <script>
  alert('Login Failed');
  </script>
  <?php
}
else {
  echo"All fields are required!";
}
There isn't one - probably because you forgot the closing curly bracket for teh if it should be "attached" to.
Move the closing curly bracket and correct the indentation to match.
   
Quote:
I basically tried many things by the help of some websites and videos but still couldn't solved it.

Basically, you need to read your code carefully.
if(count($row)!==0)
{
  $databasepassword = $row[0]['password'];
  if($databasepassword === $password) {
    ?>
    <script>
    alert('Login Successful');
    </script>
    <?php
  }
  else {
    ?>
    <script>
    alert('Login Failed');
    </script>
    <?php
  }
  // lets guess a } is missing here
  else {
    echo"All fields are required!";
  }
}

Lets guess that a } is misplaced.
   

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