Can anyone tell me why if Else is not returning Is it?

Asked

Viewed 67 times

0

Come on, guys, I’ll try to be as enlightening as possible on the question. Next: I have an if Else that is done by a Pdo query, when I type an existing Cpf it does the job properly, until then ok! But when I type in a Cpf that doesn’t exist in the database, it should display Else, where it’s not doing that, someone can help me?

CODE

$acao  = (isset($_POST['acao'])) ? $_POST['acao'] : '';
$name  = (isset($_POST['name'])) ? $_POST['name'] : '';
$indicated_by  = (isset($_POST['indicated_by'])) ? $_POST['indicated_by'] : '';
$password  = (isset($_POST['password'])) ? $_POST['password'] : '';
$cpf   = (isset($_POST['cpf'])) ? str_replace(array('.','-'), '',         
$_POST['cpf']): '';
$telefone   = (isset($_POST['telefone'])) ? str_replace(array('(',')','','-'), '', $_POST['telefone']): '';
$data_cadastro   = (isset($_POST['data_cadastro'])) ? str_replace(array('.','-'), '', $_POST['data_cadastro']): '';
$password  = (isset($_POST['password'])) ? $_POST['password'] : '';
$id_ind = (isset($_POST['id_ind'])) ? $_POST['id_ind'] : '';


$conexao = conexao::getInstance();
$sql = "SELECT * FROM users WHERE cpf = ".$cpf;
$stm = $conexao->prepare($sql);
$stm->execute();
$clientes = $stm->fetchAll(PDO::FETCH_OBJ);
foreach($clientes as $cliente):

    if($cliente->cpf === $cpf):
        CPF EXISTE


    else:
        CPF NÃO EXISTE

    endif;

endforeach;

1 answer

0


You make a search in the database looking for the CPF informed, so if vc type a CPF that does not exist in the base your select will not return any record, making it not enter nor the foreach.

You can directly check if the query returned any value, if it returns the CPF exists, if it returns nothing the CPF does not exist

$conexao = conexao::getInstance();
$sql = "SELECT * FROM users WHERE cpf = ".$cpf;
$stm = $conexao->prepare($sql);
$stm->execute();
$clientes = $stm->fetchAll(PDO::FETCH_OBJ);

if($clientes ):
    CPF EXISTE
else:
    CPF NÃO EXISTE
endif;

Thus the if checks if there is any value for the variable $clientes, if there is, the CPF has been found, if there is no CPF.

  • Aaaah my friend, agr I realized where I was wrong... being that I saw her tested this way, but I did not notice that I was making a comparison of information in the if. Like... I was doing this in if: if($clients === $Cpf) No need kkkkkkkk, but now it worked. Man thanks so much for the attention there, I think the mental exhaustion was getting in the way of not fixing it. Obg for the help, vlw even.

Browser other questions tagged

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