Unset does not work

Asked

Viewed 120 times

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'.

  • @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.

  • you can also give a header('Location: '.$_SERVER['PHP_SELF']); it will redirect to the same page and will not have the $_POST

1 answer

0

Tereza, you may have to create the file with php code on another page. Or, use Javascript with AJAX. (I’ve had a similar problem with this)

I will put the answer only with the use of PHP. And then I’ll leave the link to the answer to a question I asked (about a problem similar to this only using Javascript/jQuery.

Reply with PHP:

Here you would have to create another file (Let’s say it will be the send.php file).

<form action="enviar.php" method="POST">

  <input type="text" name="nome" />

</form>



<!-- Arquivo enviar.php -->


<?php
 

    if($_POST) 
    {

    $name = $_POST['nome'];


      $inserindo = "INSERT INTO nome_da_tabela(nome_do_campo) VALUES('$name');";
	
	$resultado_insert = mysqli_query($conn,  $inserindo);
         

     }
      
     header(location:"pagina-do-seu-form.html");
    

?>

  <!-- O HEADER vai redirecionar para página que contém seu form -->

Here is the link of the question (asked by me) with the answer, but using Javascript.

Link

Browser other questions tagged

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