I've got a login for a project that I'm trying to figure out. I've got a values by POST (through an AJAX call), I've already checked if the username entered exists and up to there, it works well. But know I want to check if the password is valid for that username. Here's the PHP code:
<?php
    //File with the conection data
    include_once "../conexion.php";
    //Initialization
    $user = "";
    $password = "";
    $errors = "";
    $result = "";
    $result2 = "";
    //Some validations (I've edited a little to make it shorter)
    if((isset($_POST['user'])) && (!empty($_POST['user']))){
        $user = $_POST['user'];
    }else{
        $errors .= "blablablah";
    }
    if((isset($_POST['password'])) && (!empty($_POST['password']))){
            $password = $_POST['password'];
        }else{
            $errors .= "blablabla";
        }
    //I make the query
    $sql = "SELECT user FROM users WHERE user = ?";
    //I prepare the query
    if($stmt = $con->prepare($sql)){
         $stmt->bind_param('s', $user);
         $result = $stmt->execute();
    }
    /* UP TO HERE, if I check that $result is true and echo "User exists" or something like that, IT WORKS, AS THE USER EXISTS */
    /* BUT now I want to check the PASSWORD, given that the user exists */
        if($result){
            //I make the query
            $sql2 = "SELECT user, password FROM users WHERE user = ? AND password = ?";
            //I prepare the query
            if($stmt2 = $con->prepare($sql2)){
                $stmt2->bind_param('ss', $user, $password);
                $result2 = $stmt2->execute();
                if($result2){
                    echo "ENTERED";
                }else{
                    echo "PASSWORD OR USER INCORRECT";
                }
            }
        } 
?>
I'm using the result of those echos in the success function in the AJAX call, here's the code for that (there's an onClick event (onClick="login()") in the button of the form, and validationLogin() has all the valitations for the fields --> all that works fine):
function login(){
if(validationLogin()){
        $.ajax({
                url: "http://localhost/myProject/extras/Login.php", 
                type: "POST",
                data: {"user": user, 
                       "password": password, 
                       },
                dataType: "html",
                cache: false,
                beforeSend: function() {    
                    console.log("Processing...");
                },
                success: 
                      function(data){
                        alert(data);
                        console.log(data);
                    }
    });
}else{
    //alert("Incorrect fields");
}
}
This returns EMPTY, I alert the data just to check what it has... the alert is empty, don't understand why :/
I've tried this idea --> PHP mySQL check if username and password are in the database but in that case it keeps saying that it's incorrect :/
A few notes:
- I know that the passwords should be encrypted, will probably use md5 later on.
- By using the echos in the PHP file and the alert(data) / console.log(data) in the JS file, I just want to check if it works, in order to proceed. Perhaps there are other ways, better ways of doing all this, I know, but I like to go little by little
- I'm really trying to understand what I code, then will improve on it, I really want to understand how and why it functions or not
- I would like to continue using prepared statements
Thanks everyone in advance! :)
 
    