Skip to content

Instantly share code, notes, and snippets.

@uchoamaster
Created May 19, 2025 22:38
Show Gist options
  • Save uchoamaster/861a925cb3834dd25714b5f753fec17e to your computer and use it in GitHub Desktop.
Save uchoamaster/861a925cb3834dd25714b5f753fec17e to your computer and use it in GitHub Desktop.
edit projeto agenda v2
<?php
// Incluir arquivo de conexão
require_once 'config/database.php';
// Inicializar variáveis
$nome = $telefone = $email = "";
$nome_err = $telefone_err = $email_err = "";
// Verificar se o ID foi passado
if (isset($_GET["id"]) && !empty(trim($_GET["id"]))) {
// Obter parâmetro da URL
$id = trim($_GET["id"]);
// Verificar se há mensagem de erro
if (isset($_GET["error"]) && $_GET["error"] == "empty_fields") {
$nome_err = $telefone_err = "Este campo é obrigatório";
}
// Preparar uma declaração select
$sql = "SELECT * FROM contatos WHERE id = ?";
if ($stmt = mysqli_prepare($conn, $sql)) {
// Vincular variáveis à declaração preparada como parâmetros
mysqli_stmt_bind_param($stmt, "i", $param_id);
// Definir parâmetros
$param_id = $id;
// Tentar executar a declaração preparada
if (mysqli_stmt_execute($stmt)) {
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) == 1) {
// Buscar linha de resultado como um array associativo
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
// Recuperar valores de campos individuais
$nome = $row["nome"];
$telefone = $row["telefone"];
$email = $row["email"];
} else {
// URL não contém ID válido
header("location: index.php");
exit();
}
} else {
echo "Oops! Algo deu errado. Por favor, tente novamente mais tarde.";
}
// Fechar declaração
mysqli_stmt_close($stmt);
}
// Fechar conexão
mysqli_close($conn);
} else {
// URL não contém parâmetro de ID
header("location: index.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Editar Contato - Agenda Telefônica</title>
<link rel="stylesheet" href="assets/css/style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
<header>
<div class="container">
<div class="header-content">
<div class="logo">
<i class="fas fa-address-book fa-2x"></i>
<h1>Agenda Telefônica</h1>
</div>
</div>
</div>
</header>
<div class="container">
<h2><i class="fas fa-edit"></i> Editar Contato</h2>
<div class="form-container">
<form action="update.php" method="post">
<div class="form-group">
<label for="nome">Nome</label>
<input type="text" id="nome" name="nome" value="<?php echo $nome; ?>" placeholder="Digite o nome completo">
<?php if (!empty($nome_err)): ?>
<span class="error-text"><?php echo $nome_err; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="telefone">Telefone</label>
<input type="text" id="telefone" name="telefone" value="<?php echo $telefone; ?>" placeholder="Ex: (11) 98765-4321">
<?php if (!empty($telefone_err)): ?>
<span class="error-text"><?php echo $telefone_err; ?></span>
<?php endif; ?>
</div>
<div class="form-group">
<label for="email">Email (opcional)</label>
<input type="email" id="email" name="email" value="<?php echo $email; ?>" placeholder="Ex: [email protected]">
<?php if (!empty($email_err)): ?>
<span class="error-text"><?php echo $email_err; ?></span>
<?php endif; ?>
</div>
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-buttons">
<button type="submit" class="btn btn-success">
<i class="fas fa-save"></i> Atualizar
</button>
<a href="index.php" class="btn">
<i class="fas fa-arrow-left"></i> Voltar
</a>
</div>
</form>
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment