0
I am learning php object oriented and wanted to know how to put if
to check whether data already exists in the database
<?php
include "head.php";
if(isset($_POST['email'])){
include 'class/classEmpresa.php';
$empresa = new empresa();
$verificacnpj = $empresa->verificaCnpjExiste($_POST['cnpj'],$conectaBanco);
$id_empresa = $empresa->cadastraEmpresa($_POST['nome_fantasia'],$_POST['cnpj'],$_POST['telefone'],$_POST['email'],$conectaBanco);
include 'class/classPessoa.php';
$pessoa = new pessoa();
$verificacpf = $pessoa->verificaCpfExiste($_POST['cpf'],$conectaBanco);
$id_pessoa = $pessoa ->cadastraPessoa($_POST['nome'],$_POST['cpf'],$_POST['email'],$_POST['telefone'],$id_empresa,$conectaBanco);
include 'class/classUsuario.php';
$usuario = new usuario();
$verificaEmail = $usuario->verificaEmailExiste($_POST['email'],$conectaBanco);
$cadastraUsuario = $usuario ->cadastraUsuario($_POST['email'],$_POST['senha'],$id_pessoa,$conectaBanco);
include 'class/classHtml.php';
$msg = new html();
$mostrar = $msg->mostrarScript('cadastrado com sucesso!');
echo ($mostrar);
}
?>
You need to be careful with those "if there is" checks. If you want to enter a new record, the correct way is to try to enter and fail if there is one, to avoid race condition. An error that I see in almost all ""home systems""" in PHP (and in some other languages, with less frequency) is the person doing the separate test of the insertion, which allows two records to be entered if they occur "almost at the same time".
– Bacco