4
Hi, I’m trying to create a page to change the registration data with mysql database, but it should only update the data that are filled and not change the other data if it already exists in the database.
I’ve tried a thousand ways and I can’t, they could help me?
Follows the code:
<?php
try{
//Abre a conexão;
include "conexao.php";
//Pega os dados do formulário;
$campos = array();
$id = $_POST['id'];
if(!empty($nome)) $campos[]="nome='$nome'";
if(!empty($apelido)) $campos[]="apelido='$apelido'";
if(!empty($telefone)) $campos[]="telefone='$telefone'";
if(!empty($celular)) $campos[]="celular='$celular'";
if(!empty($email)) $campos[]="email='$email'";
if(!empty($endereco)) $campos[]="endereco='$endereco'";
if(!empty($num_end)) $campos[]="num_end='$num_end'";
//Prepara a query;
$stmt = $conn -> prepare("UPDATE `clientes` SET ".implode(',', $campos)." WHERE ID = :id");
//Executa a query;
$stmt->execute([':id' => $id]);
echo "Dados atualizados com sucesso! :)";
}
//Exibe a mensagem caso dê erro;
catch(PDOException $e) {
echo $stmt;
echo 'Error: ' . $e->getMessage();
}
?>
If you have solved it, post the solution in the answers section. Changing the question invalidates the existing answers. You can check the history here to copy the solution in the appropriate field: https://answall.com/posts/249405/revisions
– Bacco
Thanks, I’m new here and I didn’t know that yet.
– Rodrigo Alves