Insert MySQL PDO

4

Well, I need to do an insert and I use PHP OO, however, how should I do this? According to my code it is registering, but it is not working perfect.

Am I doing something wrong? You do not get any error messages, just say you are successfully registered!

        try {
                   $sql = $conn->prepare("INSERT INTO tbl_PESSOAS (
                                       COD_IDENT_IGREJ,
                                       TXT_NOMEX_PESSO,
                                       TXT_FONEX_PESSO,
                                       DAT_NASCI_PESSO,
                                       FLG_SEXOX_PESSO,
                                       FLG_IDENT_PESSO,
                                       TXT_EMAIL_PESSO,
                                       TXT_SENHAX_USUAR,
                                       TXT_ENDER_CEPXX,
                                       TXT_ENDER_BAIRR,
                                       TXT_ENDER_LOGRA,
                                       TXT_ENDER_NUMER,
                                       TXT_ENDER_COMPL,
                                       COD_IDULT_ATUAL,
                                       DAT_ULTIM_ATUAL)
                               VALUES (:COD_IDENT_IGREJ,
                                       :TXT_NOMEX_PESSO,
                                       :TXT_FONEX_PESSO,
                                       :DAT_NASCI_PESSO,
                                       :FLG_SEXOX_PESSO,
                                       :FLG_IDENT_PESSO,
                                       :TXT_EMAIL_PESSO,
                                       :TXT_SENHAX_USUAR,
                                       :TXT_ENDER_CEPXX,
                                       :TXT_ENDER_BAIRR,
                                       :TXT_ENDER_LOGRA,
                                       :TXT_ENDER_NUMER,
                                       :TXT_ENDER_COMPL,
                                       :COD_IDULT_ATUAL,
                                       :DAT_ULTIM_ATUAL)");

        $collection = array(
            'COD_IDENT_IGREJ' => $codIgreJ,
            'TXT_NOMEX_PESSO' => $lbl_NOME,
            'TXT_FONEX_PESSO' => $lbl_TELEFONE,
            'DAT_NASCI_PESSO' => $dte_ANIVERSARIO,
            'FLG_SEXOX_PESSO' => $lbl_SEXO,
            'FLG_IDENT_PESSO' => $lbl_FLAG_IDENT,
            'TXT_EMAIL_PESSO' => $lbl_EMAIL,
            'TXT_SENHAX_USUAR' => $lbl_SENHA,
            'TXT_ENDER_CEPXX' => $lbl_CEP,
            'TXT_ENDER_BAIRR' => $lbl_BAIRRO,
            'TXT_ENDER_LOGRA' => $lbl_RUA,
            'TXT_ENDER_NUMER' => $lbl_NUMERO,
            'TXT_ENDER_COMPL' => $lbl_COMPLEMENTO,
            'COD_IDULT_ATUAL' => $lbl_COD_ID,
            'DAT_ULTIM_ATUAL' => $lbl_NOW
        );

        $sql->execute($collection);
        /* $UltAiAdd = $conn->lastInsertId();

          $sql2 = $conn->prepare("INSERT INTO tbl_PESSOA_CELULA VALUES ( COD_IDENT_PESSO =  :COD_IDENT_PESSO, COD_IDENT_CELUL = :COD_IDENT_CELUL, FLG_IDENT_PESSO = :FLG_IDENT_PESSO, "
          . "COD_IDULT_ATUAL = :COD_IDULT_ATUAL, DAT_ULTIM_ATUAL = :DAT_ULTIM_ATUAL)");
          $sql2->bindParam(":COD_IDENT_PESSO", $UltAiAdd);
          $sql2->bindParam(":COD_IDENT_CELUL", $codCel);
          $lbl_FLAG_PE = 'M';
          $sql2->bindParam(":FLG_IDENT_PESSO", $lbl_FLAG_PE);
          $sql2->bindParam(":COD_IDULT_ATUAL", $lbl_COD_ID);
          $sql2->bindParam(":DAT_ULTIM_ATUAL", $lbl_NOW);

          $sql->execute(); */
    } catch (Exception $exc) {
        error_log($exc->getMessage());
        echo "058"; //Erro no cadastro.
    }
    echo "057"; //Cadastrado com sucesso!
    
asked by anonymous 24.08.2015 / 16:07

2 answers

1

The problem is that you were merging the UPDATE structure with query :

To insert use:

INSERT INTO TABELA (campo1, campo2) VALUES (:campo1, :campo2)

To update use:

UPDATE TABELA SET campo1=?, campo2=? WHERE campo_where:campo_where


    $stmt = $conn->prepare("INSERT INTO tbl_PESSOAS (
                                           COD_IDENT_IGREJ,
                                           TXT_NOMEX_PESSO,
                                           TXT_FONEX_PESSO,
                                           DAT_NASCI_PESSO,
                                           FLG_SEXOX_PESSO,
                                           FLG_IDENT_PESSO,
                                           TXT_EMAIL_PESSO,
                                           TXT_SENHAX_USUAR,
                                           TXT_ENDER_CEPXX,
                                           TXT_ENDER_BAIRR,
                                           TXT_ENDER_LOGRA,
                                           TXT_ENDER_NUMER,
                                           TXT_ENDER_COMPL,
                                           COD_IDULT_ATUAL,
                                           DAT_ULTIM_ATUAL)
                                   VALUES (:COD_IDENT_IGREJ,
                                           :TXT_NOMEX_PESSO,
                                           :TXT_FONEX_PESSO,
                                           :DAT_NASCI_PESSO,
                                           :FLG_SEXOX_PESSO,
                                           :FLG_IDENT_PESSO,
                                           :TXT_EMAIL_PESSO,
                                           :TXT_SENHAX_USUAR,
                                           :TXT_ENDER_CEPXX,
                                           :TXT_ENDER_BAIRR,
                                           :TXT_ENDER_LOGRA,
                                           :TXT_ENDER_NUMER,
                                           :TXT_ENDER_COMPL,
                                           :COD_IDULT_ATUAL,
                                           :DAT_ULTIM_ATUAL)");

$collection = array(
  ':COD_IDENT_IGREJ' => $codIgreJ,
  ':TXT_NOMEX_PESSO' => $lbl_NOME,
  ':TXT_FONEX_PESSO' => $lbl_TELEFONE,
  ':DAT_NASCI_PESSO' => $dte_ANIVERSARIO,
  ':FLG_SEXOX_PESSO' => $lbl_SEXO,
  ':FLG_IDENT_PESSO' => $lbl_FLAG_IDENT,
  ':TXT_EMAIL_PESSO' => $lbl_EMAIL,
  ':TXT_SENHAX_USUAR'=> $lbl_SENHA,
  ':TXT_ENDER_CEPXX' => $lbl_CEP,
  ':TXT_ENDER_BAIRR' => $lbl_BAIRRO,
  ':TXT_ENDER_LOGRA' => $lbl_RUA,
  ':TXT_ENDER_NUMER' => $lbl_NUMERO,
  ':TXT_ENDER_COMPL' => $lbl_COMPLEMENTO,
  ':COD_IDULT_ATUAL' => $lbl_COD_ID,
  ':DAT_ULTIM_ATUAL' => $lbl_NOW
);
$conn->execute($stmt, $collection);
$UltAiAdd = $conn->lastInsertId();
    
24.08.2015 / 16:31
1
  

SQLSTATE [HY093]: Invalid parameter number: number of bound variables does not match number of tokens in

It means that in insert there are no more columns specified than values, in case you have a bind that is not mapped to a column that is :TXT_FONEX_PESSO

    
24.08.2015 / 19:25