Hi, I am trying to create a page to change the cadastral data with mysql database, but it should only update the data that is filled and do not change the other data if they already exist in the database.
I've tried a thousand ways and I can not, could you help me?
Follow 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();
}
?>