Php and Pdo - Data insertion are being duplicated

Asked

Viewed 111 times

0

This question has already scored here and there on the forums and, most of the time, the programmer really was, for several reasons, commanding the Insert twice.

But I guess it’s not my case.

I’m using PHP and accessing an Oracle 11 database.

I have Inserts and selects.

The problem: At each Insert the target table in Oracle shows the same data twice.

I have tried 'n' approaches, without success. My 'output' was to write an absurd code, to search for duplicities and delete them. And I still have one additional problem: deleting the values entirely from the table in Oracle (DELETE FROM MINHATABELA;), directly in sql-editor, yet these data continue to be listed in the PHP select!

My PHP routine that causes the insertion is like this:

<?
require("PDOConnection.php");
$dbh = new PDOConnection();

//....trecho da alimentação das variáveis de insert omitido

    $arrayDados = [
    'num_pedido' => $numeroPedido,
    'email_redator' => $emailRedator,
    'email_solicitante' => $emailSolicitante,
    'int_setor' => $intSetor,
    'int_natureza' => $intNatureza,
    'int_programa' => $intPrograma,
    'txt_solicitacao' => $txtDescricao,
    'data_prevista' => $dataPrevista,
    'hora_prevista' =>$txtHoraPrevista,
    'intStatus' => 2
    ];

$sentenca = "INSERT INTO MINHATABELA  (num_pedido,email_redator,email_solicitante,int_setor,int_natureza,int_programa,txt_solicitacao,data_prevista,hora_prevista,intStatus) 
    VALUES (:num_pedido,:email_redator,:email_solicitante,:int_setor,:int_natureza,:int_programa,:txt_solicitacao,:data_prevista,:hora_prevista,:intStatus)";   
$resultado=$dbh->insert($sentenca,$arrayDados);
echo $resultado;
?>

My Insert method is like this:

public function insert($sql,$arrayDados) {
$stmt = $this->dbh->prepare($sql);
$stmt->execute($arrayDados);
return $stmt->rowCount();
}//insert
  • Maybe the problem is how many times the PHP file that causes the insertion is included/required.

  • In fact, there is no magic, either the Insert is called more than once or it occurs by other means. There is no Trigger that can trigger this other Insert?

  • @tvdias, there are no triggers, as it is a table created only for testing. It is in the standard of common tables, without operational sophistication.

  • I’ve already modified the. htaccess on Apache not to redirect more than once, I’ve checked (as you suggested) if the favicon check could be computed as an extra call, but none of this interfered.

2 answers

0

You can try this way, getting the array through the filter_input_array and passing the positions that will be inserted in your bank.

   public function insert(array $dados) {
  $this->dados  = filter_input_array(INPUT_POST, FILTER_DEFAULT);
  if(!empty($this->dados)) {
     unset($this->dados['submit']); // Elimina a posição do botão criada no array
     $sql = $dbh->prepare('INSERT INTO MINHATABELA(campo1, campo2, campo3) VALUES (:campo1, :campo2, :campo3)');
     $sql->bindParam(':campo1', $this->dados['valorInputFormulario']); //Recebe o array preenchido no formulario e pega o valor. Ex:$this->dados['nome']
     $sql->bindParam(':campo2', $this->dados['valorInputFormulario']);
     $sql->bindParam(':campo3', $this->dados['valorInputFormulario']);
     $sql->execute();
  }
}
  • I’ve done it, dear @Valdir_silva

0

I use it like this and work normally, see if it helps you.

$query = "INSERT INTO tbale (cp1, cp2) VALUES ('val1', 'val2')";

$rs = $conn->prepare($query);
$rs->execute();
  • I’ve done it, dear @Jonathanbrambati

Browser other questions tagged

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