How to make php return status?

Asked

Viewed 130 times

0

next. I am doing a validation with angular and php, for when a user is registering, be checked if such user and password already exist and is not registered again. And I need php to return some information to angular so that a message can be displayed if there is a user with the same data.

my php:

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

include_once("conPDO.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

$nome = $data->nome;
$email = $data->email;
$senha = $data->senha;
$idCep = $data->idCep;
$tipoUsuario = "C";

$verificaUsuario=$pdo->prepare("SELECT * FROM usuarios WHERE nome=:nome AND email=:email");
$verificaUsuario->bindValue("nome", $nome); 
$verificaUsuario->bindValue("email", $email); 
$verificaUsuario->execute();

$quant = $verificaUsuario->rowCount();

if($quant != 1){

$insereUsuario=$pdo->prepare("INSERT INTO usuarios (idUsuario, idCep, tipoUsuario, nome, email, senha) VALUES (?, ?, ?, ?, ?, ?)");
$insereUsuario->bindValue(1, NULL); 
$insereUsuario->bindValue(2, $idCep); 
$insereUsuario->bindValue(3, $tipoUsuario); 
$insereUsuario->bindValue(4, $nome);
$insereUsuario->bindValue(5, $email);
$insereUsuario->bindValue(6, $senha);

$insereUsuario->execute();

}else{
   O que colocar aqui?
}

?>

1 answer

1

One solution is to give a false return: return false:

else {
echo 'A conta já existe!'; 
return false;
}

And also change $quant != 1 for $quant < 1, if you do not want to include two records in the bank you do that, se $quant menor que (<) 1 returns true, if does not return false.

See if it helps you...

  • But beyond that, I need some information to go back to the angle so I can display this message on the screen, you know?

  • 1

    I understand, sorry, I thought you wanted to return to the PHP, I will look to know here also I do update in my post.

  • 3

    @Gustavosevero it is interesting that you add this information also in the question, to avoid wrong answers. Reading quickly, I imagined a solution similar to William’s.

Browser other questions tagged

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