0
I don’t want to save a record twice by giving F5 on the page.
I put a input type hidden
on the form and I’m checking if it was started to perform a Insert, and whether the Insert I’m giving away unset
in the variable $_POST
identifying the form.
<form action="#" method="POST">
<input type="hidden" name="formulario" value="1">
<input type="text" name="nome" />
</form>
<?php
$formulario = $_POST['formulario'];
if ((isset($formulario)) && ($formulario == '1')) {
$name = $_POST['nome'];
if (mysqli_query($conn, "INSERT INTO user (`nome`) VALUES ('$name')")) {
unset($formulario);
}
}
?>
Tereza, instead of giving unset, you can assign another value to her, for example, '2'.
– Rodrigo Tognin
@Rodrigotognin This will make no difference. When updating the page, a new request will be made with the same data and the same insertion will be done.
– Woss
you can also give a
header('Location: '.$_SERVER['PHP_SELF']);
it will redirect to the same page and will not have the$_POST
– Lucas