Login with CPF and CNPJ

Asked

Viewed 44 times

0

My code below is working. However, note that I own the field CL_Cpf and I need to add another that is CL_Cnpj. Therefore, I need both to work in the same field input and log in to the system.

How could I do that?

Code:

if(isset($_POST['documento']) && empty($_POST['documento']) == false) {
$documento = addslashes($_POST['documento']);
$email = addslashes($_POST['email']);

 $dsn = "mysql:dbname=Provider;host=********l";
 $dbuser = "root";
  $dbpass = "*******";

  try {
  $db = new PDO($dsn, $dbuser, $dbpass);
  $sql = $db->query("SELECT * FROM Provider.Cliente WHERE CL_Cpf =    '$documento' AND CL_Email = '$email'");
  if($sql->rowCount() > 0) {

    $dado = $sql->fetch();

   $_SESSION['documento'] = $dado['CL_Cpf'];
   $_SESSION['email'] = $dado['CL_Email'];
   header("Location: index.php");
  }
  } catch (PDOException $e) {
echo "Falhou: ".$e->getMessage();      
}  
}
?>
  • 2

    Why are there so many commas in your text? Wouldn’t it be simpler to use the correct punctuation to separate the prayers? As for the code, what do you have to say about the email? It seems to be a login field currently, but did not mention it in the question. It should be maintained or not?

  • thanks for the reply Anderson, yes the email will be maintained, the login contains Cpf and email, however I want it to contain, Cpf, cnpj and email , when the person type Cpf or cnpj he will validate and log in the system

  • 2

    https://answall.com/questions/297464/condi%C3%A7%C3%a3o-de-login-with-Usu%C3%a1rio-ou-e-mail-no-mesmo-campo

  • 1

    As @Andersoncarloswoss himself said... you can validate using OR, just taking care to validate what Cpf, cnpj or input email is... ''SELECT * FROM Provider.Cliente WHERE CL_Cpf = '$documento' OR CL_Email = '$email' OR CL_Cnpj = '$VAR'"

  • 3

    @Reynaldomansi or a WHERE '$documento' IN (CL_cpf, CL_cnpj) (see my link above)

  • @Bacco I think the option of In well even better and more practical, just wanted to illustrate the OU as a matter of logic itself

  • 1

    @Reynaldomansi improved the response of the other link to cover the 2 situations

  • 2

    @Reynaldomansi and took the opportunity to make an alert for ( ), because the OR is a danger if the person does not know how to group. Imagine the disaster if the author doesn’t pay attention to it :D

  • Thank you very much ! it would be too much for me to ask how to make the correct select ? rsrs .. would be so ??

  • $sql = $db->query("SELECT * FROM Provider.Client WHERE '$document' IN (Cl_cpf, Cl_cnpj) AND Cl_email = '$email'");

  • didn’t work !!!

  • ok solved this way $sql = $db->query("SELECT * FROM Provider.WHERE client (Cl_cpf= $document or Cl_cnpj= $document) AND Cl_email = '$email'");

Show 7 more comments
No answers

Browser other questions tagged

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