PHP does not give error, but also does not register correctly

Asked

Viewed 187 times

1

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

The login works perfectly, but does not register the new playerID. And no error, just do not register.

Follows 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 you’re not registering is SQL3. Can anyone give me a glimpse of what I’m doing wrong?

Apparently, you’re not getting past that part:

while ($stmt2->fetch()){

Thank you!

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

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 registration (here if($stmt2->num_rows <= 0){).

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

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

You must remove the while and the if just below him.

But if the following codeif($playerID != $playerID_res){ serves to validate if there is already a similar ID in the bank, it is better to insert a UNIQUE CONSTRAINT directly in the database and treat the exception.

  • Thank you very much! Now I will look for how to treat the exception!

  • I did it in a different way: I just checked if there was any playerID in the BD with that user and it worked. Now I’m going to look about UNIQUE CONSTRAINT. Actually, I would have some reading to indicate?

  • 1

    Read on the following link: Indexes and Foreign Keys. Scroll down a bit and you will find about UNIQUE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.