PDO Insert does not work

Asked

Viewed 681 times

1

Before you ask me the question, I have consulted other topics here in the OR and found no solution. I’m trying to make a simple Insert and as soon as I run the whole script just doesn’t happen, the screen remains blank. I already made a test changing(just to see if it falls in the catch) and changed the table name, then it gave error message, and I went back to the right code.

Form

<form method="POST" action="gerarpdf.php" target="_blank" enctype="multipart/form-data">
    <div id="formulario">
        <input type="text" placeholder="Código da Entidade" maxlength="5" name="numeroDocumento" required/>
        <select name="tipoDocumento" required>
            <option value="" disabled selected>Tipo do Documento</option>
            <option value="01">Volvo</option>
            <option value="02">Saab</option>
        </select>
        <select name="anoReferencia" required>
            <option value="" disabled selected>Ano de Referência</option>
            <?php 
                $data = date("Y");
                for($i = 0; $i < 3; $i++) {
                    echo '<option value="'.substr($data,2,2).'">'.$data.'</option>';
                    $data++;
                }
            ?>
        </select>
        <select name="mesReferencia" required>
            <option value="" disabled selected>Mês de Referência</option>
            <option value="01">Janeiro</option>
            <option value="02">Feveiro</option>
            <option value="03">Março</option>
            <option value="04">Abril</option>
            <option value="05">Maio</option>
            <option value="06">Junho</option>
            <option value="07">Julho</option>
            <option value="08">Agosto</option>
            <option value="09">Setembro</option>
            <option value="10">Outubro</option>
            <option value="11">Novembro</option>
            <option value="12">Dezembro</option>
        </select>
        <select name="codProjeto" required>
            <option value="" disabled selected>Projeto</Projeto>
            <option value="01">Teste 1</option>
            <option value="02">Teste 2</option>
            <option value="03">Teste 3 </option>
        </select>
        <input type="file" name="file" class="arquivo">
    </div>
    <input type="submit" value="Gerar Código" required/>
</form>

gerarpdf.php

/*Display de erros*/
  ini_set('display_errors', true); 
  error_reporting(E_ALL);

//Recebe o campo hidden, que informa a quantidade de campos inseridos no formulário
  $limiteForBarcode = 1;
  if(!empty($_POST['quantidadeCampos'])) {
    $limiteForBarcode = $_POST['quantidadeCampos'];
  }
  /*
   * Manipulação do banco de dados
   */
  $bancoDeDados = new Bd();
  try {
    $numeroDocumento = $_POST["numeroDocumento"];
    $tipoDocumento = $_POST["tipoDocumento"];
    $anoReferencia = $_POST["anoReferencia"];
    $mesReferencia = $_POST["mesReferencia"];
    $codProjeto = $_POST["codProjeto"];
    $nomeArquivo = $_FILES["file"];
    $code = $tipoDocumento.$numeroDocumento.$anoReferencia.$mesReferencia.'001';
    $bancoDeDados->createEntidade($numeroDocumento,$tipoDocumento,$anoReferencia,$mesReferencia,$nomeArquivo,$code,$codProjeto);
  }
  catch (PDOException $e) {
    var_dump($err->getMessage());
  }

bd.php

/*Display de erros*/
  ini_set('display_errors', true); 
  error_reporting(E_ALL);

    public function 
createEntidade($numeroDocumento,$tipoDocumento,$anoReferencia,$mesReferencia,$nomeArquivo,$code,$codProjeto) {
        $stmt = $this->con->prepare("INSERT INTO barcode_entidades(tipo_documento, codigo_entidade, codigo_projeto, ano_ref, mes_ref, caminho, nome_arquivo, barcode)VALUES(:tipo_documento,:codigo_entidade,:codigo_projeto,:ano_ref,:mes_ref,:caminho,:nome_arquivo,:barcode);");
        $stmt->bindParam(':tipo_documento', $tipoDocumento);
        $stmt->bindParam(':codigo_entidade', $numeroDocumento);
        $stmt->bindParam(':codigo_projeto', $codProjeto);
        $stmt->bindParam(':ano_ref', $anoReferencia);
        $stmt->bindParam(':mes_ref', $mesReferencia);
        $stmt->bindParam(':caminho', $nomeArquivo["tmp_name"]);
        $stmt->bindParam(':nome_arquivo', $nomeArquivo["name"]);
        $stmt->bindParam(':barcode', $code);
        $stmt->execute();
        if($stmt->rowCount() > 0) {
            echo "cadastro com sucesso";
        }
        else {
            echo "não foi cadastrado";
        }
    }
  • 1

    Blank screen means that errors are not being displayed, put at the beginning of the code these two lines: ini_set('display_errors', true); error_reporting(E_ALL); then post the mistakes.

  • @lost Placed and still showed nothing :(. I ran an Insert test on Pgadmin and it worked.

  • this may help you, put after running $stmt: var_dump($stmt->errorInfo()); // this will show you errors regarding SQL if any, source: http://php.net/manual/en/pdostatement.errorinfo.php

  • 2

    @Lost just realized that I was trying to send 14 characters to a 13 column. I removed one of them and the Insert worked. You weren’t supposed to get any PHP error messages ?

  • @Leonardobosquett My code already had this line and I received nothing :S.

1 answer

2


Even with the try catch and ini_set('display_errors', true); error_reporting(E_ALL); it did not show the error. I realized I was trying to insert 14 characters in a 13 column. I modified this and it worked normally.

Browser other questions tagged

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