mysql select query does not fetch columns

I am trying to log in with php and mysql using the post method, only that when I go to press submit the sql query cannot find the records that exist, and therefore cannot log in this is my code:

<?php

session_start();
  $hostname="";
  $username="";
  $password="";
  $dbname="";
  $conn = new mysqli($hostname,$username, $password, $dbname); 

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
  ?> <script> console.log("Connection to db failed");</script><?
}

    $username = $_POST["username"];
    $password = $_POST["password"];

    $sql = "SELECT username, password FROM admin WHERE utente = '$username' AND password = '$password'";
   if($result = $conn->query($sql)){
        if($result->num_rows > 0)
        echo 'sei loggato!!!';
    else
       echo 'non sei loggato!!!';
   }
?>

Could someone tell me what I'm doing wrong? (I did not use prepared statements because it is only a test)

1 answer

  • answered 2021-04-21 16:55 harunur rashid

        <?php
    
    session_start();
      $hostname="localhost";
      $username="root";
      $password="";
      $dbname="test";
      $conn = new mysqli($hostname,$username, $password, $dbname); 
    
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
      echo'<script> console.log("Connection to db failed");</script>';
    }
    
        $username = $_GET["username"];
        $password = $_GET["password"];
    
        $sql = "SELECT username, password FROM admin WHERE username = '$username' AND password = '$password'";
       if($result = $conn->query($sql)){
            if($result->num_rows > 0)
            echo 'sei loggato!!!';
        else
           echo 'non sei loggato!!!';
       }
    ?>