login failed with php oop with ajax

40
October 20, 2021, at 03:50 AM

I have done login page using php oops with ajax for signin button. if(isset($result["username"])!="") Its return wrong details in Json type. if(isset($result["username"])=="") its return success in Json type. please help me when i give wrong user deatils if(isset($result["username"])=="") its return success and enter into welcome.php page. When I give correct user deatils if(isset($result["username"])!="") its return wrong details. Please help me to find this error

Signin.php:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Log in</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="assests/style.css" rel="stylesheet">
    <script src="assests/jquery-1.11.1.min.js"></script>
    <script src="assests/bootstrap.min.js"></script>
    <style>
      form{
        padding-left:30%;
        padding-top:8%;
      }
      h4{
        padding-left:25%;
        padding-bottom:5%;
      }
    </style>
</head>
<body>
<form class="form-horizontal" action='javascript:void(0)' method="POST" id="form">
  <fieldset>
    <div  id="legend">
      <h4>Sign In</h4>
    </div>
    <div class="control-group">
      <!-- Fullname -->
      <label class="control-label"  for="username">Username</label>
      <div class="controls">
        <input type="text" id="username" name="username" placeholder="" class="input-xlarge" required="true">
      </div>
    </div>

    <div class="control-group">
      <!-- Password-->
      <label class="control-label" for="password">Password</label>
      <div class="controls">
        <input type="password" id="password" name="password" placeholder="" class="input-xlarge" required="true">
      </div>
    </div>
 
 
    <div class="control-group">
      <!-- Button -->
      <div class="controls">
        <button class="btn btn-success" type="submit"  id="submit" name="signin">Signin</button>
        <input type="hidden" name="type" value="show" id="hidden" >
      </div>
    </div>
 <div class="control-group">
      <!-- Button -->
      <div class="controls">
      Not Registered yet? <a href="index.php">Register Here</a>
      </div>
    </div>
  </fieldset>
</form>
<script type="text/javascript">
                        $(document).ready(function(){
                            $("#form").submit(function(){
                                var username=$("#username").val();
                                var password=$("#password").val();
                                    if(username!="" && password!=""){
                                        $.ajax({
                                            url:"login.php",
                                            data:$("#form").serialize(),
                                            type:"POST",
                                            dataType:"JSON",
                                                success:function(data){
                                                    if(data.return=="success"){
                                                        window.location.href="welcome.php";
                                                    }
                                                        else{
                                                            alert(data.return);
                                                        }
                                                }
                                        })
                                    }
                            });
                        });
</script>
</body>
</html>

Login.php:

<?php
session_start();
include("config.php");

$data=new Database();
if(isset($_POST['type']) && ($_POST['type']) =="show"){
    $username=$_POST["username"];
    $password=$_POST["password"];
    $result=$data->fetch("tblusers","*", $username, $password);
        if(isset($result["username"])==""){
            $_SESSION["username"]=$username;
            echo json_encode(['return'=>"success"]);
        }
        else{
            echo Json_encode(['return'=>"wrong Details"]);
        }
} 
?>

config.php: function name(fetch())

<?php
   
   class Database 
   {
      private $servername = "localhost";
      private $username   = "root";
      private $password   = "";
      private $dbname = "crud_oops";
      public $con;
      public $customerTable = "customers";
      public function __construct()
      {
         try {
            $this->con = new mysqli($this->servername, $this->username, $this->password, $this->dbname);   
         } catch (Exception $e) {
            echo $e->getMessage();
         }
      }
      // Insert customer data into customer table
      public function insertRecond($name, $email, $username, $dob)
      {
         $sql = "INSERT INTO $this->customerTable (name, email, username, dob) VALUES('$name','$email','$username','$dob')";
         $query = $this->con->query($sql);
         if ($query) {
            return true;
         }else{
            return false;
         }
      }
      // Fetch customer records for show listing
      public function displayRecord()
      {
         $sql = "SELECT * FROM $this->customerTable";
         $query = $this->con->query($sql);
         $data = array();
         if ($query->num_rows > 0) {
            while ($row = $query->fetch_assoc()) {
               $data[] = $row;
            }
            return $data;
         }else{
            return false;
         }
      }
      // Fetch single data for edit from customer table
      public function getRecordById($id)
      {
         $query = "SELECT * FROM $this->customerTable WHERE id = '$id'";
         $result = $this->con->query($query);
         if ($result->num_rows > 0) {
            $row = $result->fetch_assoc();
            return $row;
         }else{
            return false;
         }
      }

      public function totalRowCount(){
         $sql = "SELECT * FROM $this->customerTable";
         $query = $this->con->query($sql);
         $rowCount = $query->num_rows;
         return $rowCount;
      }
   
      // Update customer data into customer table
      public function updateRecord($id, $name, $email, $username, $dob)
      {
         $sql = "UPDATE $this->customerTable SET name = '$name', email = '$email', username = '$username', dob = '$dob' 
         WHERE id = '$id'";
         $query = $this->con->query($sql);
         if ($query) {
            return true;
         }else{
            return false;
         }
      }
      // Delete customer data from customer table
      public function deleteRecord($id)
      {
         $sql = "DELETE FROM $this->customerTable WHERE id = '$id'";
         $query = $this->con->query($sql);
         if ($query) {
            return true;
         }else{
            return false;
         }
      }
      
// for username availblty
      public function usernameavailblty($uname) {
      $result =mysqli_query($this->con,"SELECT Username FROM tblusers WHERE Username='$uname'");
      return $result;
         
   }
   
   // Function for registration
      public function registration($fname,$uname,$uemail,$pasword)
      {
      $ret=mysqli_query($this->con,"insert into tblusers(FullName,Username,UserEmail,Password) values('$fname','$uname','$uemail','$pasword')");
      return $ret;
      }
   
   // Function for signin
      public function fetch( $table,$condition,$username,$password){
      $sql="SELECT * FROM tblusers WHERE Username='$username' AND Password='$password'";
      $result=$this->con->query($sql);
      $row=$result->fetch_assoc();
      return $row;
      }
   }
?>
Answer 1

In the Login.php

if(isset($result["username"])==""){

change to

if(isset($result[0]["username"])==""){

It looks like in the config.php, function fetch returns an associative arrays

To help you debug, try to var_dump($row) in this method

READ ALSO
Getting Empty File with AWS Lambda and ALB

Getting Empty File with AWS Lambda and ALB

I have used the Npm package, http-proxy-middleware

58
Trying to multiply and make another row in Pandas Dataframe

Trying to multiply and make another row in Pandas Dataframe

How do I make another row called 'Sum'), which contains the sum of for each order line (quantity, multiplied by price)in Pandas

59
Append method to add text to body not working [duplicate]

Append method to add text to body not working [duplicate]

This is such a simple command and I am loading jQuery beforehandOther jQuery on the page is working as well

68
Add &lt;a&gt; href to img with appendto

Add <a> href to img with appendto

I'm hoping someone can help

63