0

I have my login page where i need to check whether that username and password exists in my database or not So i have fired select query where username and password is entered by the user. Now I want is when there are no rows in result that means Username or Password is incorrect.

This header method is not working.What i need to do?

$sql="SELECT * FROM login WHERE username='$username' and password='$password'";
$result=mysqli_query($con,$sql);

$count=mysqli_num_rows($result);
if($count==0)
{
echo "Login Failed: Please Try Again";
}
if($count==1)
{
header('Location:something.php');
}
1
  • check return data from ajax and redirect via js window.location Commented Aug 15, 2015 at 10:46

1 Answer 1

2

You already have an ajax call. Use this function as a callback:

function success(message) {
    if (message === "success") {
        window.location.href = yourpath; //yourpath is a string involving something.php
    } else {
        alert(message);
    }
}

And change your PHP to something like:

$sql="SELECT * FROM login WHERE username='$username' and password='$password'";
$result=mysqli_query($con,$sql);

$count=mysqli_num_rows($result);
if ($count==0) {
    echo "Login Failed: Please Try Again";
} else if ($count==1) {
    echo "success";
}

When you manage to make this work, you should fix other issues as well, namely:

  • your code is vulnerable to SQL injection, therefore your site can easily be hacked
  • you did not encrypt the password, which is a security leak
Sign up to request clarification or add additional context in comments.

2 Comments

This code is not working.The problem remains still same.
Try to integrate it successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.