When I do the UPDATE it saves in the bank correctly, but when I refresh the page the data comes out of the bank

0

I'm trying to retrieve data from a table inside an input where the user can be updated and save to the same table through UPDATE. I was able to do the UPDATE, but I edit the data by input and send, it saves in the right bank, but when I refresh the page it erases all the bank's fields.

 if (isset($_POST)){
        $sql = "UPDATE cadastros SET nome = '{$_POST['nome']}', email = '{$_POST['email']}', telefone = '{$_POST['telefone']}', site = '{$_POST['site']}', endereco = '{$_POST['endereco']}', bairro = '{$_POST['bairro']}', cidade = '{$_POST['cidade']}', estado = '{$_POST['estado']}', cep = '{$_POST['cep']}', produtos = '{$_POST['produtos']}', descricao = '{$_POST['descricao']}', nomeCliente = '{$_POST['nomeCliente']}' WHERE idusuario = '$idusuario'";
        mysql_query($sql);

    }
    $sql = "SELECT * FROM cadastros WHERE idusuario = '$idusuario'";
    $result = mysql_query($sql);
    $registro = mysql_fetch_assoc($result);

    $nome =             $registro['nome'];
    $email =            $registro['email'];
    $telefone =         $registro['telefone'];
    $site =             $registro['site'];
    $endereco =         $registro['endereco'];
    $bairro =           $registro['bairro'];
    $cidade =           $registro['cidade'];
    $estado =           $registro['estado'];
    $cep =              $registro['cep'];
    $produtos =         $registro['produtos'];
    $descricao =        $registro['descricao'];
    $nomeCliente =      $registro['nomeCliente'];
?>


   <!-- FORMULÁRIO -->
   <form class='tab-pane transition scale fade in active' autocomplete="off" id='myForm' method="POST">

        <div class="field">
            <label for="doge" class="field-label">Seu nome completo:</label>
            <input type="text" id="doge" name="nomeCliente"  required="" class="field-input" value="<?php echo $nomeCliente ?>">
        </div>                    
        <div class="field">
            <label for="doge" class="field-label">Nome da loja:</label>
            <input type="text" id="doge" name="nome"  required="" class="field-input" value="<?php echo $nome ?>">
        </div>                 
        <div class="field">
            <label for="doge" class="field-label">E-mail loja:</label>
            <input type="text" id="doge" name="email"  required="" class="field-input" value="<?php echo $email ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Telefone:</label>
            <input type="text" id="doge" name="telefone"  required="" class="field-input" OnKeyPress="formatar('##-#########', this); return somenteNumero(event)" maxlength="12" value="<?php echo $telefone ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Site:</label>
            <input type="text" id="doge" name="site"  required="" class="field-input" value="<?php echo $site ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Endereço com número:</label>
            <input type="text" id="doge" name="endereco"  required="" class="field-input" value="<?php echo $endereco ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Bairro:</label>
            <input type="text" id="doge" name="bairro"  required="" class="field-input" value="<?php echo $bairro ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Cidade:</label>
            <input type="text" id="doge" name="cidade"  required="" class="field-input" value="<?php echo $cidade ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Estado</label>
            <input type="text" id="doge" name="estado"  required="" class="field-input" value="<?php echo $estado ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">CEP:</label>
            <input type="text" id="doge" name="cep"  required="" class="field-input" value="<?php echo $cep ?>">
        </div>
        <div class="field">
            <label for="doge" class="field-label">Produtos e marcas que você atende:</label>
            <input type="text" id="doge" name="produtos"  required="" class="field-input" value="<?php echo $produtos ?>">
        </div>
      <div class="field">
            <label for="doge" class="field-label">Descrição:</label>
            <textarea type="text" id="doge" name="descricao"  required="" class="field-input" value="<?php echo $descricao ?>"></textarea>
      </div>
    <div class="space-top-2x clearfix">
        <button type="submit" class="btn btn-success pull-right"><i class="flaticon-correct7"></i> Enviar</button>
    </div>               

      </form> 
    
asked by anonymous 02.08.2016 / 17:53

2 answers

2

As probably the post already exists on the page, the isset returns true and the data is updated with the existing value, which in this case is empty.

Try before this function check via var_dump() what comes in the post or what the result of var_dump(isset($_POST))

Maybe you can help this Thiago Belem post on empty and isset .

    
02.08.2016 / 19:19
0

Try to get the posts before working on the update as follows:

function checkPosts(){
                $retorno = true;
                $camposParaChecar = array('nome','email','telefone');
                foreach($_POST as $key => $val){
                        if (in_array($key, $camposParaChecar) && empty($val)) { 
                        $retorno = false;
                        break;
                        }
                }
                return $retorno;
        }
if (checkPosts()){
        $sql = "UPDATE cadastros SET nome = '{$_POST['nome']}', email = '{$_POST['email']}', telefone = '{$_POST['telefone']}', site = '{$_POST['site']}', endereco = '{$_POST['endereco']}', bairro = '{$_POST['bairro']}', cidade = '{$_POST['cidade']}', estado = '{$_POST['estado']}', cep = '{$_POST['cep']}', produtos = '{$_POST['produtos']}', descricao = '{$_POST['descricao']}', nomeCliente = '{$_POST['nomeCliente']}' WHERE idusuario = '$idusuario'";
        mysql_query($sql);

    }
//restante do código

In the variable

$camposParaChecar = array('nome','email','telefone');

Place the fields you want to check if they are filled in, if you prefer to have the check occur in all fields, replace the foreach inside the function with this:

foreach($_POST as $val){
    if (empty($val)) { 
        $retorno = false;
        break;
    }
}

I hope this helps.

Good luck!

    
02.08.2016 / 19:43