Skip to content

Commit

Permalink
Merge branch 'hotfix/double-edit-entry'
Browse files Browse the repository at this point in the history
Corrige a função de edit duplicar a entrada.
  • Loading branch information
comatic0 committed Aug 21, 2024
2 parents 46af7a3 + 6fb2231 commit edc4dd8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
28 changes: 10 additions & 18 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,20 @@ function addTable($pdo, $nome, $descricao, $nome_do_mestre, $numero_max_jogadore
}
}

function addTableForm($pdo) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$nome = $_POST['nome'];
$descricao = $_POST['descricao'];
$nome_do_mestre = $_POST['nome_do_mestre'];
$numero_max_jogadores = $_POST['numero_max_jogadores'];
$categoria = $_POST['categoria'] === 'Outro' ? $_POST['categoria_custom'] : $_POST['categoria'];

addTable($pdo, $nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria);

header('Location: index.php');
exit();
}
}

addTableForm($pdo);

function updateTable($pdo, $id, $nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria) {
try {
$stmt = $pdo->prepare("UPDATE mesas SET nome = ?, descricao = ?, nome_do_mestre = ?, numero_max_jogadores = ?, categoria = ? WHERE id = ?");
return $stmt->execute([$nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria, $id]);
$result = $stmt->execute([$nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria, $id]);

if ($result) {
error_log("Table with ID $id updated successfully.");
} else {
error_log("Failed to update table with ID $id.");
}

return $result;
} catch (PDOException $e) {
error_log("PDOException: " . $e->getMessage());
return false;
}
}
Expand Down
16 changes: 13 additions & 3 deletions views/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
$mesa = getTableById($pdo, $id);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$id = $_POST['id'];
if (!is_numeric($id)) {
die('Invalid ID');
}
$nome = $_POST['nome'];
$descricao = $_POST['descricao'];
$nome_do_mestre = $_POST['nome_do_mestre'];
$numero_max_jogadores = $_POST['numero_max_jogadores'];
$categoria = $_POST['categoria'] === 'Outro' ? $_POST['categoria_custom'] : $_POST['categoria'];

updateTable($pdo, $id, $nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria);
$result = updateTable($pdo, $id, $nome, $descricao, $nome_do_mestre, $numero_max_jogadores, $categoria);

header('Location: index.php');
exit();
if ($result) {
header('Location: index.php');
exit();
} else {
error_log("Failed to update table with ID $id.");
echo "Failed to update table.";
}
}
?>
<?php include '../includes/header.php'; ?>
Expand All @@ -30,6 +39,7 @@
<p>Página de edição de mesa. Formulários marcados com * são obrigatórios.</p>
<div class="form-container">
<form method="post">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($mesa['id']); ?>">
<label for="nome">Nome:</label>
<input type="text" id="nome" name="nome" value="<?php echo htmlspecialchars($mesa['nome']); ?>" required>
<label for="descricao">Descrição:</label>
Expand Down

0 comments on commit edc4dd8

Please sign in to comment.