Malý repozitář do hodin WTL pro 3.J skupinu WEB
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
1.7 KiB

<?php
require "header.php";
$title = "";
$author = "";
$errors = [];
if ($_SERVER["REQUEST_METHOD"]=="POST"){
$title = trim($_POST["title"] ?? "");
$author = trim($_POST["author"] ?? "");
if (empty($title)) {
$errors [] = "Nazev knihy je povinny";
}
if (empty($author)) {
$errors [] = "Nazev autora je povinny";
}
if (empty($errors)){
try {
$query = $pdo->prepare("INSERT INTO books (title,author)
VALUES (:title, :author)");
$query->execute(["title"=>$title,"author"=>$author]);
$lastId = $pdo->lastInsertId();
header("Location: index.php?status=added&id=".$lastId);
exit;
} catch (PDOException $e) {
$errors[]= "Chyba při ukládní do databáze:". html_escape($e->getMessage());
}
}
}
?>
<h4>Přidat novou knihu</h4>
<?php if(!empty($errors)):?>
<div class="alert alert-danger" role="alert">
<b>Vhyb ve formuláří nebo při ukládání:</b>
<ul>
<?php foreach($errors as $error):?>
<li><?= $error ?></li>
<?php endforeach;?>
</ul>
</div>
<?php endif;?>
<form action="add_books.php" method="post">
<div class="mb-3">
<label for="title" class="form-label">Název knihy</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="mb-3">
<label for="" class="form-label">Název Autora</label>
<input type="text" class="form-control" id="author" name="author" required>
</div>
<button type="submit" class="btn btn-primary">Odeslat</button>
<a href="/" class="btn btn-secondary">Zrušit</a>
</form>