Show sourcecode
The following files exists in this folder. Click to view.
db-test.php
ovn_form1.php
ovn_form2.php
ovn_form3.php
ovn_form4.php
ovn_form5.php
ovn_form6.php
ovn_form7.php
ovn_form8.php
ovn_fragetavling.php
ovn_funk1.php
ovn_funk2.php
ovn_funk3.php
ovn_funk4.php
ovn_funk5.php
ovn_funk6.php
ovn_gr1.php
ovn_gr2.php
ovn_gr3.php
ovn_gr4.php
ovn_gr5.php
svar/
tarningsduellen.php
db-test.php
773 lines UTF-8 Unix (LF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
<?php
error_reporting(-1);
ini_set('display_errors', 1);
ini_set('output_buffering', 0);
// ===============================
// ANSLUTNING TILL DATABASEN
// ===============================
$host = "localhost";
$dbname = "mindatabas";
$username = "root";
$password = "";
try {
$pdo = new PDO(
"mysql:host=$host;dbname=$dbname;charset=utf8mb4",
$username,
$password
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("Kunde inte ansluta till databasen.");
}
// Funktion som skyddar text när den skrivs ut i HTML
function h($text) {
return htmlspecialchars($text, ENT_QUOTES, "UTF-8");
}
// Meddelande som kan visas efter en operation
$message = "";
// ==========================================
// LÄGG TILL FÖRFATTARE
// ==========================================
if (isset($_POST["add_author"])) {
$namn = trim($_POST["namn"]);
if ($namn !== "") {
$sql = "INSERT INTO forfattare (namn)
VALUES (:namn)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"namn" => $namn
]);
header("Location: db-test.php?message=author_added");
exit;
}
}
// ==========================================
// UPPDATERA FÖRFATTARE
// ==========================================
if (isset($_POST["update_author"])) {
$id = $_POST["id"];
$namn = trim($_POST["namn"]);
$sql = "UPDATE forfattare
SET namn = :namn
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"namn" => $namn,
"id" => $id
]);
header("Location: db-test.php?message=author_updated");
exit;
}
// ==========================================
// TA BORT FÖRFATTARE
// ==========================================
if (isset($_POST["delete_author"])) {
$id = $_POST["id"];
try {
$sql = "DELETE FROM forfattare
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"id" => $id
]);
header("Location: db-test.php?message=author_deleted");
exit;
} catch (PDOException $e) {
// Om författaren används av en bok kommer FOREIGN KEY
// normalt att stoppa borttagningen.
header("Location: db-test.php?message=author_in_use");
exit;
}
}
// ==========================================
// LÄGG TILL BOK
// ==========================================
if (isset($_POST["add_book"])) {
$titel = trim($_POST["titel"]);
$utgivningsar = $_POST["utgivningsar"];
$forfattare_id = $_POST["forfattare_id"];
$sql = "INSERT INTO bocker
(titel, utgivningsar, forfattare_id)
VALUES
(:titel, :utgivningsar, :forfattare_id)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"titel" => $titel,
"utgivningsar" => $utgivningsar,
"forfattare_id" => $forfattare_id
]);
header("Location: db-test.php?message=book_added");
exit;
}
// ==========================================
// UPPDATERA BOK
// ==========================================
if (isset($_POST["update_book"])) {
$id = $_POST["id"];
$titel = trim($_POST["titel"]);
$utgivningsar = $_POST["utgivningsar"];
$forfattare_id = $_POST["forfattare_id"];
$sql = "UPDATE bocker
SET titel = :titel,
utgivningsar = :utgivningsar,
forfattare_id = :forfattare_id
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"titel" => $titel,
"utgivningsar" => $utgivningsar,
"forfattare_id" => $forfattare_id,
"id" => $id
]);
header("Location: db-test.php?message=book_updated");
exit;
}
// ==========================================
// TA BORT BOK
// ==========================================
if (isset($_POST["delete_book"])) {
$id = $_POST["id"];
$sql = "DELETE FROM bocker
WHERE id = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute([
"id" => $id
]);
header("Location: db-test.php?message=book_deleted");
exit;
}
// ==========================================
// HÄMTA ALLA FÖRFATTARE
// ==========================================
$sql = "SELECT *
FROM forfattare
ORDER BY namn";
$stmt = $pdo->query($sql);
$authors = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ==========================================
// HÄMTA ALLA BÖCKER + FÖRFATTARNAMN
// ==========================================
$sql = "SELECT
bocker.id,
bocker.titel,
bocker.utgivningsar,
bocker.forfattare_id,
forfattare.namn AS forfattare
FROM bocker
JOIN forfattare
ON bocker.forfattare_id = forfattare.id
ORDER BY bocker.titel";
$stmt = $pdo->query($sql);
$books = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ==========================================
// MEDDELANDEN
// ==========================================
if (isset($_GET["message"])) {
switch ($_GET["message"]) {
case "book_added":
$message = "Boken har lagts till.";
break;
case "book_updated":
$message = "Boken har uppdaterats.";
break;
case "book_deleted":
$message = "Boken har tagits bort.";
break;
case "author_added":
$message = "Författaren har lagts till.";
break;
case "author_updated":
$message = "Författaren har uppdaterats.";
break;
case "author_deleted":
$message = "Författaren har tagits bort.";
break;
case "author_in_use":
$message = "Författaren kan inte tas bort eftersom det finns böcker kopplade till författaren.";
break;
}
}
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Mitt bibliotek</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial, sans-serif;
background: #f4f6f8;
color: #222;
}
header {
background: #1e293b;
color: white;
padding: 35px 20px;
}
header div {
max-width: 1100px;
margin: auto;
}
header h1 {
margin: 0 0 8px;
font-size: 2.2rem;
}
header p {
margin: 0;
color: #cbd5e1;
}
main {
max-width: 1100px;
margin: 30px auto;
padding: 0 20px 50px;
}
h2 {
margin-top: 0;
}
.message {
background: #dcfce7;
border: 1px solid #86efac;
padding: 14px;
border-radius: 8px;
margin-bottom: 25px;
}
.grid {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 25px;
align-items: start;
}
.card {
background: white;
border-radius: 12px;
padding: 22px;
box-shadow: 0 3px 12px rgba(0,0,0,0.08);
margin-bottom: 25px;
}
label {
display: block;
margin-top: 12px;
margin-bottom: 5px;
font-weight: bold;
}
input,
select {
width: 100%;
padding: 10px;
border: 1px solid #cbd5e1;
border-radius: 6px;
font-size: 1rem;
}
button {
border: 0;
border-radius: 6px;
padding: 9px 13px;
cursor: pointer;
font-size: 0.9rem;
}
.primary {
background: #2563eb;
color: white;
margin-top: 16px;
}
.primary:hover {
background: #1d4ed8;
}
.save {
background: #16a34a;
color: white;
}
.delete {
background: #dc2626;
color: white;
}
.books {
display: grid;
gap: 15px;
}
.book {
padding: 18px;
border: 1px solid #e2e8f0;
border-radius: 10px;
}
.book h3 {
margin: 0 0 8px;
}
.book-info {
color: #64748b;
margin-bottom: 15px;
}
.edit-row {
display: grid;
grid-template-columns: 2fr 100px 1.5fr auto auto;
gap: 8px;
align-items: center;
}
.author-row {
display: flex;
gap: 8px;
margin-bottom: 10px;
}
.author-row input {
flex: 1;
}
.author-row form {
display: flex;
gap: 8px;
width: 100%;
}
.danger-info {
font-size: 0.85rem;
color: #64748b;
margin-top: 15px;
}
footer {
text-align: center;
color: #64748b;
padding: 30px;
}
@media (max-width: 850px) {
.grid {
grid-template-columns: 1fr;
}
.edit-row {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<header>
<div>
<h1>📚 Mitt bibliotek</h1>
<p>
En dynamisk webbsida byggd med PHP och MySQL
</p>
</div>
</header>
<main>
<?php if ($message !== ""): ?>
<div class="message">
<?= h($message) ?>
</div>
<?php endif; ?>
<div class="grid">
<!-- ========================= -->
<!-- FÖRFATTARE -->
<!-- ========================= -->
<section>
<div class="card">
<h2>Lägg till författare</h2>
<form method="post">
<label for="namn">
Namn
</label>
<input
type="text"
id="namn"
name="namn"
required
>
<button
type="submit"
name="add_author"
class="primary"
>
Lägg till
</button>
</form>
</div>
<div class="card">
<h2>Författare</h2>
<?php foreach ($authors as $author): ?>
<div class="author-row">
<form method="post">
<input
type="hidden"
name="id"
value="<?= $author["id"] ?>"
>
<input
type="text"
name="namn"
value="<?= h($author["namn"]) ?>"
>
<button
type="submit"
name="update_author"
class="save"
>
Spara
</button>
<button
type="submit"
name="delete_author"
class="delete"
onclick="return confirm('Vill du verkligen ta bort författaren?')"
>
Ta bort
</button>
</form>
</div>
<?php endforeach; ?>
<p class="danger-info">
En författare som har böcker kan inte tas bort
eftersom FOREIGN KEY skyddar relationen.
</p>
</div>
</section>
<!-- ========================= -->
<!-- BÖCKER -->
<!-- ========================= -->
<section>
<div class="card">
<h2>Lägg till bok</h2>
<form method="post">
<label for="titel">
Titel
</label>
<input
type="text"
id="titel"
name="titel"
required
>
<label for="utgivningsar">
Utgivningsår
</label>
<input
type="number"
id="utgivningsar"
name="utgivningsar"
required
>
<label for="forfattare_id">
Författare
</label>
<select
id="forfattare_id"
name="forfattare_id"
required
>
<?php foreach ($authors as $author): ?>
<option value="<?= $author["id"] ?>">
<?= h($author["namn"]) ?>
</option>
<?php endforeach; ?>
</select>
<button
type="submit"
name="add_book"
class="primary"
>
Lägg till bok
</button>
</form>
</div>
<div class="card">
<h2>Böcker</h2>
<div class="books">
<?php foreach ($books as $book): ?>
<div class="book">
<h3>
<?= h($book["titel"]) ?>
</h3>
<div class="book-info">
<?= h($book["forfattare"]) ?>
·
<?= h($book["utgivningsar"]) ?>
</div>
<form
method="post"
class="edit-row"
>
<input
type="hidden"
name="id"
value="<?= $book["id"] ?>"
>
<input
type="text"
name="titel"
value="<?= h($book["titel"]) ?>"
>
<input
type="number"
name="utgivningsar"
value="<?= h($book["utgivningsar"]) ?>"
>
<select name="forfattare_id">
<?php foreach ($authors as $author): ?>
<option
value="<?= $author["id"] ?>"
<?php
if ($author["id"] == $book["forfattare_id"]) {
echo "selected";
}
?>
>
<?= h($author["namn"]) ?>
</option>
<?php endforeach; ?>
</select>
<button
type="submit"
name="update_book"
class="save"
>
Spara
</button>
<button
type="submit"
name="delete_book"
class="delete"
onclick="return confirm('Vill du verkligen ta bort boken?')"
>
Ta bort
</button>
</form>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
</div>
</main>
<footer>
PHP + MySQL – dynamiska webbsidor
</footer>
</body>
</html>