erste push
This commit is contained in:
136
edit.php
Normal file
136
edit.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
// ID aus der URL holen und validieren
|
||||
if (!isset($_GET['index']) || !is_numeric($_GET['index'])) {
|
||||
echo "<p style='color:red;'>Ungültiger Index.</p>";
|
||||
exit;
|
||||
}
|
||||
$index = (int) $_GET['index'];
|
||||
|
||||
// JSON-Daten holen
|
||||
$data = file_get_contents('tiere.json');
|
||||
$data_array = json_decode($data, true); // true für assoziatives Array
|
||||
|
||||
// Fehlerbehandlung, falls das JSON ungültig ist oder der Index nicht existiert
|
||||
if ($data_array === null || !isset($data_array[$index])) {
|
||||
echo "<p style='color:red;'>Daten konnten nicht geladen werden oder der Index ist ungültig.</p>";
|
||||
exit;
|
||||
}
|
||||
|
||||
// Daten dem ausgewählten Index zuweisen
|
||||
$row = $data_array[$index];
|
||||
|
||||
if (isset($_POST['save'])) {
|
||||
$errors = [];
|
||||
|
||||
// Eingabedaten validieren
|
||||
if (empty($_POST['id'])) {
|
||||
$errors[] = 'ID ist erforderlich.';
|
||||
}
|
||||
if (empty($_POST['name'])) {
|
||||
$errors[] = 'Name ist erforderlich.';
|
||||
}
|
||||
if (empty($_POST['wissenschaftlicher_name'])) {
|
||||
$errors[] = 'Wissenschaftlicher Name ist erforderlich.';
|
||||
}
|
||||
if (empty($_POST['beschreibung'])) {
|
||||
$errors[] = 'Beschreibung ist erforderlich.';
|
||||
}
|
||||
if (empty($_POST['lebensraum'])) {
|
||||
$errors[] = 'Lebensraum ist erforderlich.';
|
||||
}
|
||||
if (empty($_POST['nahrung'])) {
|
||||
$errors[] = 'Nahrung ist erforderlich.';
|
||||
}
|
||||
|
||||
// Fehler anzeigen, falls vorhanden
|
||||
if (!empty($errors)) {
|
||||
foreach ($errors as $error) {
|
||||
echo "<p style='color:red;'>{$error}</p>";
|
||||
}
|
||||
} else {
|
||||
// Eingaben bereinigen, um Sicherheit zu gewährleisten
|
||||
$input = array(
|
||||
'id' => htmlspecialchars($_POST['id'], ENT_QUOTES, 'UTF-8'),
|
||||
'name' => htmlspecialchars($_POST['name'], ENT_QUOTES, 'UTF-8'),
|
||||
'wissenschaftlicher_name' => htmlspecialchars($_POST['wissenschaftlicher_name'], ENT_QUOTES, 'UTF-8'),
|
||||
'beschreibung' => htmlspecialchars($_POST['beschreibung'], ENT_QUOTES, 'UTF-8'),
|
||||
'lebensraum' => htmlspecialchars($_POST['lebensraum'], ENT_QUOTES, 'UTF-8'),
|
||||
'nahrung' => htmlspecialchars($_POST['nahrung'], ENT_QUOTES, 'UTF-8')
|
||||
);
|
||||
|
||||
// Ausgewählten Index aktualisieren
|
||||
$data_array[$index] = $input;
|
||||
|
||||
// JSON-Daten zurückschreiben
|
||||
$data = json_encode($data_array, JSON_PRETTY_PRINT);
|
||||
if (file_put_contents('tiere.json', $data) === false) {
|
||||
echo "<p style='color:red;'>Fehler beim Speichern der Daten.</p>";
|
||||
exit;
|
||||
}
|
||||
|
||||
// Umleitung zur Hauptseite
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Tiere in unserem Kleingarten</title>
|
||||
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css">
|
||||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 class="page-header text-center">Tiere in unserem Kleingarten</h1>
|
||||
<div class="row">
|
||||
<div class="col-1"></div>
|
||||
<div class="col-8"><a href="index.php" class="btn btn-primary">Zurück</a>
|
||||
<form method="POST">
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">ID</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="id" name="id" value="<?php echo htmlspecialchars($row['id'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">Name</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">Wissenschaftlicher Name</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="wissenschaftlicher_name" name="wissenschaftlicher_name" value="<?php echo htmlspecialchars($row['wissenschaftlicher_name'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">Beschreibung</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="beschreibung" name="beschreibung" value="<?php echo htmlspecialchars($row['beschreibung'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">Lebensraum</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="lebensraum" name="lebensraum" value="<?php echo htmlspecialchars($row['lebensraum'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 row">
|
||||
<label class="col-sm-2 col-form-label">Nahrung</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" class="form-control" id="nahrung" name="nahrung" value="<?php echo htmlspecialchars($row['nahrung'], ENT_QUOTES, 'UTF-8'); ?>">
|
||||
</div>
|
||||
</div>
|
||||
<input type="submit" name="save" value="Speichern" class="btn btn-primary2">
|
||||
</form>
|
||||
</div>
|
||||
<div class="col-5"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user