When I do the UPDATE it saves in the database correctly, but when I update the page the data disappears from the database

Asked

Viewed 47 times

1

I am trying to recover the data from a table inside an input where the user can be updated and save in the same table through UPDATE. I managed to do the UPDATE, but edit the data by input and send, it saves in the right database, but when I update the page it erases all fields of the database.

 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> 

2 answers

2

As probably the post already exists on the page o isset returns true and the data is updated with the existing value, which in the 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 I can help you with this post by Thiago Belem about Empty and isset.

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');

put the fields you want to check are filled in, if you prefer that the check occurs in all fields replace the foreach inside the Function by this:

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

I hope this helps.

Good luck!

Browser other questions tagged

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