How to pay for browser data after registering in the database

Asked

Viewed 72 times

0

I have a page for user registration and Internet in a single page, I need to delete the data after clicking register and insert in the bank, because if I update the page the record is duplicated in the bank, any idea?

      <?php
    if (isset($_GET['cadastra']) && $_GET['cadastra'] == 'add') {
        $cadastra = mysql_query("INSERT INTO cliente (ordemservico, nome, data,fone, produto, status, descricao) VALUES ('$ordemservico', '$_GET[nome]', '$_GET[data]', '$_GET[fone]', '$_GET[produto]', '$_GET[status]', '$_GET[descricao]')");
    if($cadastra == '1') {
        echo "Cadastrado com sucesso ! OS Número: $ordemservico <br /> <a href=\"admin.php\">Atualizar<a/>";
    }else{
        echo "Erro ao cadastrar !";
}
}
?>

Form

      <form id="form1" name"form1" method"post" action"" enctype="multipart/form-data">
     <div align="center">
     <table border="0" align="center">
         <tr>
           <td colspan="2" align="center"></td>
         </tr>
       <tr>
           <td>Nome:</td>
           <td><span id="sprytextfield1">
             <label>
               <input name="nome" type="text" id="nome" size="43" />
             </label>
<span class="textfieldRequiredMsg"><font face="Arial, Helvetica, sans-serif" size="1" color="#FF0000" >Informe seu nome</font></span></span></td>
         </tr>
           <td>Fone:</td>
           <td><label>
             <input name="fone" type="text" id="fone" size="43" />
             <br />
           </label></td>
          </tr>
           <td>Produto:</td>
           <td><span id="sprytextfield2">
             <label>
               <input name="produto" type="text" id="produto" size="43" />
             </label>
             <span class="textfieldRequiredMsg"><font face="Arial, Helvetica, sans-serif" size="1" color="#FF0000" >Informe o produto</font></span></span></td>
          </tr>
           <td>Status:</td>
           <td><label>
             <label for="status"></label>
                <select name="status" id="status" >
                <option value="-1" selected="selected">Selecione</option>
                  <option>Aguardando</option>
                  <option>Em Atendimento</option>
                  <option>Finalizado</option>

             </select>
             <br />
           </label></td>
          </tr>
         <tr>
           <td>Descrição:</td>
           <td><label>
             <textarea name="descricao" cols="46" rows="5" id="descricao"> </textarea>
           </label></td>
         </tr>
         <tr>
           <td><label for="data"></label>
            <input type="hidden" name="data" id="data" value="<?php echo date('Y-m-d')?>" /></td>
           <td align="right"><label>
             <input type="hidden" name="cadastra" value="add" />
             <input type="submit" name="add" id="add" value="  Cadastrar  " />
            </label></td>
         </tr>
       </table>
   </form>
  • Make a http redirect right after registering in DB instead of displaying content. You can redirect to the same page, which also works.

  • @Bacco in my code now I’m doing a refresh of 2 seconds "<meta HTTP-EQUIV='refresh' CONTENT='2;URL=clients.php'>" but it doesn’t seem like a good idea, you know tell me how to make this redirect?

  • You can delete the form data after the Insert, and when it is updated with F5, your message will be displayed stating to fill in the fields.

  • @Bacco the code is as you described but when registering I get the error: "Warning: Cannot Modify header information - headers already sent by (output Started at C: xampp htdocs folder admin cadastro_clientes.php:196) in C: xampp htdocs folder admin cadastro_clientes.php on line 220"

  • @Rafaelassmann this part is easy, and it is a sign that you are on the right track. Now just take out all the things that send information on the PHP screen before giving the redirect. See more here: http://answall.com/questions/4251/

  • Understood, I got it here! Thank you so much for the support, 10! @Bacco

Show 1 more comment

1 answer

2

A possible solution without major changes in the code is this:

if($cadastra == '1') {
   // Montamos o caminho para o mesmo script:
   $url = 'http://'.$_SERVER["SERVER_NAME"].'/'.$_SERVER["PHP_SELF"];
   // Deixamos a mensagem para depois do redirect:
   $mensagem = urlencode( 'Cadastro feito com sucesso' );
   // Redirecionamos repassando a mensagem adiante:
   header( "Location: $url?mensagem=$mensagem" );
   // E encerramos o script neste ponto:
   die();
}else{
   echo "Erro ao cadastrar !";
}

Then, just add something of this type to show the message only after redirect:

if ( isset( $_GET['mensagem'] ) ) {
   echo htmlentities( $mensagem );
}

(this second part is outside the if which inserts the data as it will be executed separately)

Browser other questions tagged

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