PHP does not give error, but does not register correctly

1

I have a login, that if the user logs in from another cell phone, I register the new playerID (onesignal) on my DB.

The login works fine but does not register the new playerID. And not of the mistake at all, it just does not register.

Follow the code:

<?php
header('Access-Control-Allow-Origin: *');

include 'conn-login.php';

if($_SERVER['REQUEST_METHOD'] == 'GET'){

if($_GET["acao"]=="login"){

    $emailL = $_GET['email'];
    $senhaL = $_GET['senha'];
    $playerID = $_GET['playerID'];
    $modelo = $_GET['modelo'];
    $sistema = $_GET['sistema'];        
    $cadastro = 'Facebook';
    $face_user_id = $_GET['face_id'];
    $datahora = date('Y-m-d h:i:s');

    $sql = "SELECT id, nome, img_user, email, senha FROM usuarios WHERE (email =?) LIMIT 1";
    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('s', $emailL);
    $stmt->execute();
    $stmt->bind_result($id_user, $nome, $img_user, $email_res, $senha);
    $stmt->store_result();

    if($stmt->num_rows <> 0){

        while ($stmt->fetch()){
            // checks if the user actually exists(true/false returned)
            if (password_verify($senhaL, $senha)){

                $var = Array(
                    'status' => 'OK',
                    'id' => $id_user,
                    'nome' => $nome,
                    'img_user' => $img_user,
                    'msg' => 'Logado com sucesso!'
                );

                $sql2 = "SELECT playerID FROM playersID WHERE (id_usuario =? AND playerID =?)";
                $stmt2 = $mysqli->prepare($sql2);
                $stmt2->bind_param('is', $id_user, $playerID);
                $stmt2->execute();
                $stmt2->bind_result($playerID_res);
                $stmt2->store_result();

                if($stmt2->num_rows <= 0){

                    while ($stmt2->fetch()){

                        if($playerID != $playerID_res){

                            //INSERE O PLAYERID EM OUTRA TABELA
                            $sql3 = "INSERT INTO playersID (id_usuario, modelo, sistema, playerID, data) VALUES (?, ?, ?, ?, ?)";
                            $stmt3 = $mysqli->prepare($sql3);
                            $stmt3->bind_param('issss', $id_user, $modelo, $sistema, $playerID, $datahora);
                            $stmt3->execute();

                        } 

                        $stmt3->close(); 
                    }
                }

            } else {

                $var = Array(
                    'status' => 'ERRO',
                    'msg' => 'Usuário não cadastrado e/ou senha incorreta!'
                );
            }

            $stmt2->close(); 

        }

    $stmt->close();

    } else {

        $var = Array(
            'status' => 'ERRO',
            'msg' => 'Usuário não cadastrado e/ou senha incorreta!'
        );
    };

    echo json_encode($var);
}
}    

?>

What is not registering, is SQL3. Can anyone give me a light of what I'm doing wrong?

Apparently, this is not happening:

while ($stmt2->fetch()){

Thank you!

Note: it is via GET, but later I will change to POST.

    
asked by anonymous 11.05.2018 / 20:02

1 answer

2

Your problem is here:

if($stmt2->num_rows <= 0){
   while ($stmt2->fetch()){
        if($playerID != $playerID_res){

You are validating if there is any player registered (here if($stmt2->num_rows <= 0){) .

If it does not exist, you are running while on the returned results (in this line while ($stmt2->fetch()){ ). But, there is no returned results, because there is no player. That is, while is exactly the opposite of previous validation.

Consequently, if there are no results, this if($playerID != $playerID_res){ line will always be different and does not need to exist. For, never will return any result.

You should remove while and if just below it.

But if the following code if($playerID != $playerID_res){ is to validate if an equal ID already exists in the database, it is better to insert a UNIQUE CONSTRAINT directly into the database and handle the exception.

    
11.05.2018 / 20:13