<?php
include_once '../main.php';
$messages_error = [];
$messages = [];
if (!isset($_GET['id'])) {
exit ('Kein Datensatz unter dieser ID');
}
if (!empty($_POST)) {
// Prüfen ob Variable definiert, leer oder gleich 0 ? (true / false).
if (empty($_POST['title']) || empty($_POST['vorname']) || empty($_POST['nachname']) || empty($_POST['str']) || empty($_POST['plz']) || empty($_POST['ort']) || empty($_POST['email']) || empty($_POST['phone1'])) {
// Einer oder mehrere Werte sind leer.
$messages_error[] = 'Bitte Formular vollständig ausfüllen.';
}
// Prüfen ob Email korrekt
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$messages_error[] = 'Bitte E-Mail-Adresse prüfen.';
}
// Bei Fehlern
if (!empty($messages_error)) {
$account = $_POST;
}
else {
// Daten aus $_POST speichern (Code wie create.php + bei execute anfangs für die id "NULL" einsetzen, damit nicht unter der zum Klonen aufgerufenen id gespeichert wird)
$stmt = $pdo->prepare('INSERT INTO accounts (id, title, vorname, nachname, str, snr, plz, ort, email, phone1) VALUES (?,?,?,?,?,?,?,?,?,?)');
$stmt->execute([NULL, $_POST['title'], $_POST['vorname'], $_POST['nachname'], $_POST['str'], $_POST['snr'], $_POST['plz'], $_POST['ort'], $_POST['email'], $_POST['phone1']]);
header ('Location: ../saved.php');
exit;
}
}
else {
// Werte aus Datenbank
$stmt = $pdo->prepare('SELECT * FROM accounts WHERE id = ?');
$stmt->execute([$_GET['id']]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<?=template_header('Update')?>
<div class="content update">
<h2>Adresse ändern</h2>
<h3><?=$account['nachname']?></h3>
<!-- <form action="update.php?id=<?=$account['id']?>" method="post"> -->
<form action="" method="post">
<div class="tabelle">
<div class="fl">
<label for="title">Anrede</label><br>
<input type="text" name="title" value="<?php echo isset($account['title']) ? htmlspecialchars($account['title']) : ''; ?>" /> <!-- $account, weil bei update müssen die bestehenden Felddaten angezeigt werden -->
</div>
<div class="fl">
<label for="vorname">Vorname</label><br>
<input type="text" name="vorname" value="<?php echo isset($account['vorname']) ? htmlspecialchars($account['vorname']) : ''; ?>" />
</div>
<div class="fl">
<label for="nachname">Nachname</label><br>
<input type="text" name="nachname" value="<?php echo isset($account['nachname']) ? htmlspecialchars($account['nachname']) : ''; ?>" />
</div>
<div class="fl">
<label for="str">Strasse</label><br>
<input type="text" name="str" value="<?php echo isset($account['str']) ? htmlspecialchars($account['str']) : ''; ?>" />
</div>
<div class="fl">
<label for="snr">Nummer</label><br>
<input type="text" name="snr" value="<?php echo isset($account['snr']) ? htmlspecialchars($account['snr']) : ''; ?>" />
</div>
<div class="fl">
<label for="plz">PLZ</label><br>
<input type="text" name="plz" value="<?php echo isset($account['plz']) ? htmlspecialchars($account['plz']) : ''; ?>" />
</div>
<div class="fl">
<label for="ort">Ort</label><br>
<input type="text" name="ort" value="<?php echo isset($account['ort']) ? htmlspecialchars($account['ort']) : ''; ?>" />
</div>
<div class="fl">
<label for="email">Email</label><br>
<input type="text" name="email" value="<?php echo isset($account['email']) ? htmlspecialchars($account['email']) : ''; ?>" />
</div>
<div class="fl">
<label for="phone1">Telefon P</label><br>
<input type="text" name="phone1" value="<?php echo isset($account['phone1']) ? htmlspecialchars($account['phone1']) : ''; ?>" />
</div>
<div class="fl">
<input type="submit" value="Sichern">
<?php
// Fehlermeldungen ausgeben:
echo '<div class="messages_error">';
foreach($messages_error as $message) {
echo '<p>'.htmlspecialchars($message).'</p>';
}
echo '</div>'; ?>
<?php
// Erfolgsmeldungen ausgeben:
echo '<div class="messages">';
foreach($messages as $message)
{
echo '<p>'.htmlspecialchars($message).'</p>';
}
echo '</div>'; ?>
</div>
</div>
</form>
</div>
<?=template_footer()?>