Problems inserting data into multiple tables using Pdo

Asked

Viewed 42 times

0

Hello, my case is the following, when tables are empty all fields are normally inserted more if I try to enter more data the system returns an "Alert" which occurred an error 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();
    }
}
  • Puts the error that is happening so that we can understand what the PDO is complaining.

  • then, when you do not enter other data it returns no error, it returns nothing and enters this condition.if($objRgt->queryInsertPj($_POST) == 'ok'){ header('Location: Associate.php'); }Else{ echo '<script type="text/javascript">Alert("Registration error");</script>'; }

  • The values of the fields are coming correctly? Try to insert the record outside the foreach with false data and see what the line $insereJur->execute() returns outside the if. Use the var_dump($insereJur->execute()).

  • Sorry it took me so long to answer, I had already done this to see if the fields were really coming, that’s what I explained before, when the tables are empty the script works normally, but then qnd I will insert another given script does not execute the if line($cst->execute()) and goes straight to the end of the script.

1 answer

0

Problem solved,

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

Browser other questions tagged

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