0
Good night! I would like to know how I can implement a function (maybe?) or some method to check if the fields Cpf and e-mail in my code already exist in the bank before registering them, and if it exists, display a message for example: "CPF already registered!".
Follows the code:
<?php
session_start();
include_once '../banco/conexao.php';
$SendCadPet = filter_input(INPUT_POST, 'SendCadPet', FILTER_SANITIZE_STRING);
if ($SendCadPet) {
$nome = filter_input(INPUT_POST, 'nome', FILTER_SANITIZE_STRING);
$cpf = filter_input(INPUT_POST, 'cpf', FILTER_SANITIZE_STRING);
$endereco = filter_input(INPUT_POST, 'endereco', FILTER_SANITIZE_STRING);
$cidade = filter_input(INPUT_POST, 'cidade', FILTER_SANITIZE_STRING);
$estado = filter_input(INPUT_POST, 'estado', FILTER_SANITIZE_STRING);
$telefone = filter_input(INPUT_POST, 'telefone', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
$usuario = filter_input(INPUT_POST, 'usuario', FILTER_SANITIZE_STRING);
$senha = crypt(addslashes(filter_input(INPUT_POST, 'senha', FILTER_SANITIZE_STRING)), $security_salt);
$imagem = '../avatar/default.png';
//Inserir
$res = "INSERT INTO usuarios (nome, cpf, endereco, cidade, estado, telefone, email, usuario, senha, imagem, nivel)
VALUES (:nome, :cpf, :endereco,
:cidade, :estado, :telefone, :email, :usuario, :senha, :imagem, :nivel)";
$insert = $pdo->prepare($res);
$insert->bindParam(':nome', $nome);
$insert->bindParam(':cpf', $cpf);
$insert->bindParam(':endereco', $endereco);
$insert->bindParam(':cidade', $cidade);
$insert->bindParam(':estado', $estado);
$insert->bindParam(':telefone', $telefone);
$insert->bindParam(':email', $email);
$insert->bindParam(':usuario', $usuario);
$insert->bindParam(':senha', $senha);
$insert->bindParam(':imagem', $imagem);
$insert->bindValue(':nivel', "2");
if ($insert->execute()) {
$_SESSION['msg'] = "<center><div class='alert alert-success'>Usuário Cadastrado com Sucesso!</div></center>";
header("Location: ../views/Cadastros.php");
} else {
$_SESSION['msg'] = "<center><div class='alert alert-warning'>Falha no Cadastro!</div></center>";
header("Location: ../views/Cadastros.php");
}
} else {
$_SESSION['msg'] = "<center><div class='alert alert-warning'>Falha no Cadastro!</div></center>";
header("Location: ../views/Cadastros.php");
}
NOTE: The include_once is including the connection to the bank.
Just check if the CPF already exists in the correct bank? CPF is unique.
– user60252
That’s right.....
– Alicia
Even checking before, check also after the INSERT. It is that it may occur that another process in parallel enters the same CPF moments before ("competition of processes").
– José Diz