-1
I’m trying to do a check when I want to register a customer. Before registering I want to check if the customer’s Cpf or cnpj already exists in the database. when I try to check only Cpf it works and returns a warning and when I try to check only cnpj, also it is right, but when I try to do for both, it returns me an error saying that the customer already exists while this informed data is not in the bank.
Here’s what I did
//CHECKING IF THERE IS ALREADY A CNPJ OR CPF REGISTERED
$stmt = $conn->prepare("SELECT * from rcom_clientes_clientes WHERE cnpj= ? OR cpf=?");
$stmt -> bindValue(1 ,$cnpj);
$stmt -> bindValue(2 ,$cpf);
$stmt->execute();
$count = $stmt->rowcount();
//IF THE CLIENT ALREADY EXIST
if( $count > 0 )
{
echo"<script>alert('Desculpa, esse Cliente ja
existe!');window.location.href = '../../create_client.php';</script>";
}
else
{
ECHO 'DEU';
}
Somebody give me a hand there?
If $Cpf is empty and $cnpj is filled in with a number that does not exist in the database, it returns results that $Cpf is empty in the database. Example: $Cpf = '' and $cnpj = '001': If there is no '001' in the database, it will still return all records that 'Cpf' is empty. The most correct would be to check in PHP which variable was filled ($cnpj or $Cpf) and then query only the filled variable.
– Leonardo Furlan