Checking if Username and Password is match or avai

2020-05-03 08:07发布

问题:

I'm having a problem on my code which the username and password does not match. Here is my output. When the username and password is not match, then it will give a message that it is not match , However, when the username and password match, then there will be a message that it is match here is my code below:

html code

<body>  
  <div class="container box">  
   <div class="form-group">  
    <h3 align="center">Live Username Available or not By using PHP Ajax Jquery</h3><br />  
    <label>Enter Username</label>  
    <input type="text" name="username" id="username" class="form-control" />
    <input type="password" name="password" id="password" class="form-control" />

    <span id="availability"></span>
    <br /><br />
    <button type="button" name="register" class="btn btn-info" id="register" disabled>Register</button>
    <br />
   </div>  
   <br />  
   <br />  
  </div>  
 </body>  
</html>  

script code

  <script>  
     $(document).ready(function(){  
       $('#username','#password').blur(function(){

         var username = $(this).val();
         var password = $(this).val();

         $.ajax({
          url:'check.php',
          method:"POST",
          data:{user_name:username, password:password},
          success:function(data)
          {
           if(data != '1')
           {
            $('#availability').html('<span class="text-danger">Username and Password not Match</span>');
            $('#register').attr("disabled", true);
           }
           else
           {
            $('#availability').html('<span class="text-success">Username and Password Available</span>');
            $('#register').attr("disabled", false);
           }
          }
         })

      });
     });  
    </script>

check.php - my database connection and query that fetch it from the database

 <?php  
    //check.php  
    $connect = mysqli_connect("localhost", "username", "", "dbname"); 
    if(isset($_POST["user_name"] && $_POST["password"]))
    {
     $username = mysqli_real_escape_string($connect, $_POST["user_name"]);
     $password = mysqli_real_escape_string($connect, $_POST["password"]);
     $query = "SELECT * FROM admin WHERE username = '".$username."' AND password = '".$password."' ";
     $result = mysqli_query($connect, $query);
     echo mysqli_num_rows($result);
    }
 ?>

回答1:

Notes

  • You are wide open to SQL injection. You should be using prepared statements instead. Please read How can I prevent SQL injection in PHP?
  • You should not store passwords in plain text. Instead, use PHP's password_hash and password_verify functions to hash and verify.

Answer

You are using isset incorrectly. To check that multiple values are set, separate them by commas, not with &&:

if(isset($_POST["user_name"], $_POST["password"]))

Your PHP code as it currently stands won't produce any output as it will terminate with a fatal error on that line.

In your jQuery, you're not specifying multiple selectors correctly. They should all be inside the same set of quotes:

$('#username, #password').blur(function(){

You also need to change this code, which will set both username and password values to the same thing:

var username = $(this).val();
var password = $(this).val();

to

var username = $('#username').val();
var password = $('#password').val();