0
My code has a very strange behavior to perform insertion of unsolicited data from previous data every time the page is updated. Can anyone tell me why?
- Screen output (Every time the page is updated this message appears and the same repeated insertion happens with the same values).
- Insertion PHP code (Probably the strange behavior comes from here)
<?php
require_once '../conexao/conexao.php';
if(isset($_POST['Inserir'])){
$cd_produto = $_POST['cd_produto'];
$cd_funcionario = $_POST['cd_funcionario'];
$cd_cliente = $_POST['cd_cliente'];
$tipo_pagamento = $_POST['tipo_pagamento'];
$valor_item = $_POST['valor_item'];
$quantidade = $_POST['quantidade'];
try{
$insercao = "INSERT INTO itens_venda (cd_produto,cd_funcionario,cd_cliente,
tipo_pagamento,valor_item,quantidade,data_venda)
VALUES (:cd_produto,:cd_funcionario,:cd_cliente,:tipo_pagamento,:valor_item,:quantidade,NOW())";
$insere_dados = $conexao->prepare($insercao);
$insere_dados->bindValue(':cd_produto',$cd_produto);
$insere_dados->bindValue(':cd_funcionario',$cd_funcionario);
$insere_dados->bindValue(':cd_cliente',$cd_cliente);
$insere_dados->bindValue(':tipo_pagamento',$tipo_pagamento);
$insere_dados->bindValue(':valor_item',$valor_item);
$insere_dados->bindValue(':quantidade',$quantidade);
$insere_dados->execute();
} catch (PDOException $falha_insercao) {
echo "A inserção não foi feita".$falha_insercao->getMessage();
}
}
$seleciona_produto = $conexao->query("SELECT cd_produto, nome FROM produto");
$resultado_produto = $seleciona_produto->fetchAll();
$seleciona_funcionario = $conexao->query("SELECT cd_funcionario, nome FROM funcionario");
$resultado_funcionario = $seleciona_funcionario->fetchAll();
$seleciona_cliente = $conexao->query("SELECT cd_cliente, nome FROM cliente");
$resultado_cliente = $seleciona_cliente->fetchAll();
?>
- Form Code (To help understand)
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="utf-8">
<title> Cadastrar venda </title>
</head>
<body>
<form method="POST">
<p> Roupa:
<select name="cd_produto" required="">
<option value=""></option>
<?php
foreach ($resultado_produto as $v1) {
echo "<option value='{$v1['cd_produto']}'>{$v1['nome']}</option>";
}
?>
</select>
</p>
<p> Funcionário:
<select name="cd_funcionario" required="">
<option value=""></option>
<?php
foreach ($resultado_funcionario as $v2) {
echo "<option value='{$v2['cd_funcionario']}'>{$v2['nome']}</option>";
}
?>
</select>
</p>
<p> Cliente:
<select name="cd_cliente" required="">
<option value=""> </option>
<?php
foreach ($resultado_cliente as $v3) {
echo "<option value='{$v3['cd_cliente']}'>{$v3['nome']}</option>";
}
?>
</select>
</p>
<p> Pagamento:
<select name="tipo_pagamento" required="">
<option value=""> </option>
<option value="Pagamento á vista">Pagamento á vista</option>
</select>
</p>
<p> Valor do item: <input type="text" name="valor_item" step="0.01" required=""> </p>
<p> Quantidade: <input type="number" name="quantidade" size=30 required=""> </p>
<button name="Inserir"> Cadastrar venda </button>
</form>
</body>
</html>

The page that you’re looking for used information that you entered. Returning to that page might cause any action that you took to be repeated. Do you want to continue?I just understood that I’m repeating an insertion action several times because of some error that I still don’t know where it is in the code..– Cautium
When the user asks for a refresh, the browser forwards the last request to get the updated data. Notice what will happen ? Form Ubmit is sent again! One of the ways to solve is to separate your HTML from your PHP, just create a page for example receives.php and put the action of the form pointing to this page, when you finish doing the INSERT redirects to the form page.
– user60252
Another solution might be http://wbruno.com.br/php/diferenciar-refresh-f5-de-postsubmit/
– user60252
So there is no solution to be on the same page?
– Cautium
I think not, the solution of the above link is not 100% secure, the Sesssions may expire
– user60252