News-Bearbeitung gefixt: Feldnamen im Model an Controller & Formular angepasst, keine Notices mehr beim Editieren.

This commit is contained in:
Karsten Tlotzek 2025-07-08 10:38:27 +02:00
parent 5f3ac9f78d
commit 7280cb0246
4 changed files with 70 additions and 10 deletions

View File

@ -55,19 +55,44 @@ class NewsController {
}
public function editNewsForm() {
$id = $_GET['newsid'];
$news = $this->model->getNewsById($id);
$this->view->setVars(['news' => $news]);
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=News&do=showNews');
exit;
}
$id = $_GET['id'] ?? null;
if ($id) {
$news = $this->model->getNewsById($id);
$validData = [
'name' => $news['name'] ?? '',
'description' => $news['description'] ?? '',
'date' => $news['date'] ?? date('Y-m-d'),
];
$this->view->setVars(['validData' => $validData, 'id' => $id, 'errors' => []]);
}
}
public function updateNews() {
$id = $_POST['newsid'] ?? null;
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: index.php?controller=News&do=showNews');
exit;
}
$id = $_POST['id'] ?? null;
$data = [
'name' => $_POST['name'],
'beschreibung' => $_POST['beschreibung'],
'datum' => $_POST['datum'],
'name' => $_POST['name'] ?? '',
'description' => $_POST['description'] ?? '',
'date' => $_POST['date'] ?? date('Y-m-d'),
];
$errors = [];
if (empty($data['name']) || empty($data['description']) || empty($data['date'])) {
$errors['news'] = 'Bitte alle Felder ausfüllen.';
}
if (!empty($errors)) {
$this->view->setVars(['errors' => $errors, 'validData' => $data, 'id' => $id]);
$this->view->setDoMethodName('editNewsForm');
return;
}
$this->model->updateNews($id, $data);
$this->view->setDoMethodName('showEditSuccess');
}
public function deleteNews() {

View File

@ -24,9 +24,9 @@ class NewsModel extends Database {
$pdo = $this->linkDB();
$sql = "UPDATE news SET name = :name, description = :description, date = :date WHERE news_id = :news_id;";
$params = [
":name" => $news['titel'],
":description" => $news['inhalt'],
":date" => $news['datum'],
":name" => $news['name'],
":description" => $news['description'],
":date" => $news['date'],
":news_id" => $newsId
];
try {

View File

@ -0,0 +1,21 @@
<div class="inhalt">
<div class="form-container">
<h1>News bearbeiten</h1>
<?php if (!empty($errors['news'])): ?>
<div class="form-error"><?=htmlspecialchars($errors['news'])?></div>
<?php endif; ?>
<form class="form-horizontal" action="index.php" method="post">
<input type="hidden" name="controller" value="News">
<input type="hidden" name="do" value="updateNews">
<input type="hidden" name="id" value="<?=htmlspecialchars($id ?? '')?>">
<label for="name">Titel</label>
<input type="text" name="name" id="name" required value="<?=htmlspecialchars($validData['name'] ?? '')?>">
<label for="date">Datum</label>
<input type="date" name="date" id="date" required value="<?=htmlspecialchars($validData['date'] ?? date('Y-m-d'))?>">
<label for="description">Beschreibung</label>
<textarea name="description" id="description" rows="7" required><?=htmlspecialchars($validData['description'] ?? '')?></textarea>
<button class="button-register" type="submit">Änderungen speichern</button>
</form>
<a href="?controller=News&do=showNews">Zurück zur Übersicht</a>
</div>
</div>

View File

@ -0,0 +1,14 @@
<div class="inhalt">
<div class="login-success">
<h2>News erfolgreich bearbeitet!</h2>
<p>Du wirst in wenigen Sekunden zur Übersicht weitergeleitet...</p>
</div>
</div>
<script>
setTimeout(function() {
window.location.href = "?controller=News&do=showNews";
}, 2000);
</script>
<noscript>
<meta http-equiv="refresh" content="2;url=?controller=News&do=showNews">
</noscript>