skip to Main Content

trying to make the user login by getting details but not working please check this

here is my connection code

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "atm";

$conn = new mysqli($server ,$username ,$password ,"atm");

if(!$conn =""){
    echo "connection is established<br>";
}
?>

my main file code is

<?php
    require_once "../configmain.php";

    if(isset($_POST['login'])){
      $username = $_POST["username"];
      $password = $_POST["password"];
      $adminqry = mysqli_query($conn,"select 'admin_id' from 'admin_details' where STATUS = '1' and username = '$username' and password = '$password';")or die (mysqli_error($con));
      while($admin = mysqli_fetch_object($adminqry)){
        $adminid = $admin -> admin_id;
      }

      session_start();
      $_SESSION['adminid'] = $adminid;
      $adminid = $_SESSION['adminid'];
      echo $adminid;

      if(!$adminid){
        header("Location: ./admin/adminmain.php");
  exit();
      }
      else{

      echo "login error";
      }
    }

?>

checked if my connection is working or not it is working if my database is working or not but it is also working iam working with xampp server please help me

2

Answers


  1. Your problem is this line:

    if(!$conn ="")
    

    You aren’t checking to see if the $connection is empty, you’re actually setting it to an empty string.

    Try changing your check to this:

    if(!$conn->errno)
    

    This will check for any non-zero error numbers on your connection object. You can also get the connection error message with $conn->connect_error

    Login or Signup to reply.
  2. Try this

    STEP 1 : Replace your connection file by below given code

    <?php
    $server = "localhost";
    $username = "root";
    $password = "";
    $database = "atm";
    
    $conn = new mysqli($server ,$username ,$password ,$database);
    
    ?>
    

    STEP 2 : Replace your main file code by below give code

         <?php
                require_once "../configmain.php";
            
                if(isset($_POST['login'])){
                  $username = $_POST["username"];
                  $password = $_POST["password"];
                  $adminqry = mysqli_query($conn,"select 'admin_id' from 'admin_details' where STATUS = '1' and username = '$username' and password = '$password';")or die (mysqli_error($conn));
            
                  if(mysqli_num_rows($adminqry) == 1){
                      $admin = mysqli_fetch_object($adminqry);
                      $adminid = $admin -> admin_id;
                      session_start();
                      $_SESSION['adminid'] = $adminid;
            
                      if(isset($_SESSION['adminid'])){
                        header("Location: ./admin/adminmain.php");
                        exit();
                      }else{
                        echo "Login error";
                      }
            
                  }else{
                    echo "User not found.";
                  }
              }
            
     ?>
    
      //Notes:
        //(1) Check your connection file path is proper in require_once.
        //(2) I consider you want to redirect header("Location: ./admin/adminmain.php"); here if login success.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search