Problems inserting data into multiple tables using pdo

0

Hello, my case is as follows, when the tables are empty all fields are normally inserted more if I try to enter more data the system returns an "alert" that an error occurred while registering.

public function queryInsertPj($dados){
    try{
        $this->email = $dados['email'];
        $this->senha = 123456;//sha1(mt_rand(111111,999999));
        $this->status = 1;
        $this->user = 0;

        $this->razaoSocial = $this->objfc->tratarCaracter($dados['razaoSocial'], 1);
        $this->cnpj = $this->objfc->tratarCaracter($dados['cnpj'], 1);
        $this->telefone = $dados['telefone'];          
        $this->dataNasc = $dados['dtNasc'];          
        $this->dataCadastro = $this->objfc->dataAtual(2);

        $cst = $this->con->conectar()->prepare("INSERT INTO usuario (email, senha, status, user) VALUES (:email, :senha, :status, :user);");
        $cst->bindParam(":email", $this->email, PDO::PARAM_STR);
        $cst->bindParam(":senha", $this->senha, PDO::PARAM_STR);
        $cst->bindParam(":status", $this->status, PDO::PARAM_STR);
        $cst->bindParam(":user", $this->user, PDO::PARAM_STR);

        //VALIDAR CADASTRO
        $validar = $this->con->conectar()->prepare("SELECT email FROM usuario WHERE email = ?;");
        $validar->execute(array($this->email));   

        if($validar->rowCount() == 0){

            if($cst->execute()){
                //quando tento inserir o segungo dado o sistema para aqui...
                //BUSCAR ID DA TABELA USUARIO
                $search = $this->con->conectar()->prepare("SELECT max(idUsuario) as idUsuario FROM usuario");
                $search->execute();
                $busca = $search->fetchAll();

                foreach ($busca as $key) {
                    //INSERE ID DO USUÁRIO
                    $inserePes = $this->con->conectar()->prepare("INSERT INTO pessoa (idUsuario) VALUES (:idUser) ");
                    $inserePes->bindParam(":idUser", $key['idUsuario'], PDO::PARAM_INT);

                    if($inserePes->execute()){
                        //BUSCA ÚTLIMO ID DA TABELA PESSOA
                        $search2 = $this->con->conectar()->prepare("SELECT max(idPessoa) as idPessoa FROM pessoa");
                        $search2->execute();
                        $busca2 = $search2->fetchAll();

                        foreach ($busca2 as $key2) {
                            //INSERE O ÚLTIMO ID DA TABELA PESSOA
                            $insereJur = $this->con->conectar()->prepare("INSERT INTO juridica (idPessoa, razaoSocial, cnpj, telefone, dataNascimento, dataCadastro) VALUES (:idPes, :razao, :cnpj, :tel, :dtNasc, :dt)");
                            $insereJur->bindParam(":idPes", $key2['idPessoa'], PDO::PARAM_INT);
                            $insereJur->bindParam(":razao", $this->razaoSocial, PDO::PARAM_STR);                       
                            $insereJur->bindParam(":cnpj", $this->cnpj, PDO::PARAM_STR);
                            $insereJur->bindParam(":tel", $this->telefone, PDO::PARAM_STR);
                            $insereJur->bindParam(":dtNasc", $this->dataNasc, PDO::PARAM_STR);
                            $insereJur->bindParam(":dt", $this->dataCadastro, PDO::PARAM_STR);

                            if($insereJur->execute()){
                                return 'ok';
                            }else{
                                return 'erro';
                            }
                        } //END FOREACH2
                    } //END IF
                } //END FOREACH
            } //END IF
        } //END IF
    } catch (PDOException $ex) {
        return 'error '.$ex->getMessage();
    }
}
    
asked by anonymous 12.09.2018 / 14:53

1 answer

0

Problem solved,

The problem was structural in the tables (I forgot to set auto_increment), thank you very much for DNick help.

    
13.09.2018 / 19:33