mirror of
https://github.com/janunger/rheinwerk-video-training.git
synced 2024-11-21 06:33:41 +01:00
Kapitel 6 Lektion 7 gesplittet
This commit is contained in:
parent
88cf71d772
commit
a4afe1a11c
@ -12,23 +12,3 @@ function holeDatenbankverbindung()
|
||||
[PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"]
|
||||
);
|
||||
}
|
||||
|
||||
function holeLied($liedId)
|
||||
{
|
||||
$db = holeDatenbankverbindung();
|
||||
$liedStatement = $db->prepare("
|
||||
SELECT
|
||||
lieder.track,
|
||||
lieder.titel,
|
||||
cds.id AS cd_id,
|
||||
cds.name AS cdname,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM lieder
|
||||
LEFT JOIN cds ON lieder.cd_id = cds.id
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
WHERE lieder.id = :lied_id
|
||||
");
|
||||
$liedStatement->execute(['lied_id' => $liedId]);
|
||||
|
||||
return $liedStatement->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
@ -5,12 +5,8 @@ require __DIR__ . '/_application.php';
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (count($_POST) > 0) {
|
||||
$insertStatement = $db->prepare("INSERT INTO lieder (cd_id, track, titel) VALUES (:cd_id, :track, :titel)");
|
||||
$insertStatement->execute([
|
||||
'cd_id' => $_GET['id'],
|
||||
'track' => $_POST['track'],
|
||||
'titel' => $_POST['titel']
|
||||
]);
|
||||
|
||||
// TODO: Lied in die Datenbank speichern
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($_GET['id']));
|
||||
exit;
|
||||
@ -29,7 +25,7 @@ $cdStatement = $db->prepare("
|
||||
$cdStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$cd = $cdStatement->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$liederStatement = $db->prepare("SELECT * FROM lieder where cd_id = :cd_id ORDER BY track ASC");
|
||||
$liederStatement = $db->prepare("SELECT * FROM lieder where cd_id = :cd_id");
|
||||
$liederStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$lieder = $liederStatement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
|
@ -3,26 +3,20 @@
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie bearbeiten möchten.';
|
||||
}
|
||||
|
||||
$lied = holeLied($_GET['id']);
|
||||
// TODO: Lied mit Angaben zu CD und Künstler aus der Datenbank laden
|
||||
$lied = [];
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion'])) {
|
||||
if ($_POST['aktion'] === 'speichern') {
|
||||
$updateStatement = $db->prepare("UPDATE lieder SET track = :track, titel = :titel WHERE id = :lied_id");
|
||||
$updateStatement->execute([
|
||||
'track' => $_POST['track'],
|
||||
'titel' => $_POST['titel'],
|
||||
'lied_id' => $_GET['id']
|
||||
]);
|
||||
}
|
||||
if (isset($_POST['aktion']) && $_POST['aktion'] === 'speichern') {
|
||||
|
||||
// TODO: Lied in der Datenbank aktualisieren
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
|
@ -3,22 +3,20 @@
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie löschen möchten.';
|
||||
}
|
||||
|
||||
$lied = holeLied($_GET['id']);
|
||||
// TODO: Lied aus der Datenbank laden (analog zu lied_bearbeiten.php)
|
||||
$lied = [];
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion'])) {
|
||||
if ($_POST['aktion'] === 'loeschen') {
|
||||
$deleteStatement = $db->prepare("DELETE FROM lieder WHERE id = :lied_id");
|
||||
$deleteStatement->execute(['lied_id' => $_GET['id']]);
|
||||
}
|
||||
if (isset($_POST['aktion']) && $_POST['aktion'] === 'loeschen') {
|
||||
|
||||
// TODO: Lied aus der Datenbank löschen
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
|
14
Kapitel_6/Lektion_8/mediathek/_application.php
Executable file
14
Kapitel_6/Lektion_8/mediathek/_application.php
Executable file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @return PDO
|
||||
*/
|
||||
function holeDatenbankverbindung()
|
||||
{
|
||||
return new PDO(
|
||||
'mysql:dbname=mediathek;host=localhost',
|
||||
'root',
|
||||
'root',
|
||||
[PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"]
|
||||
);
|
||||
}
|
73
Kapitel_6/Lektion_8/mediathek/cd.php
Executable file
73
Kapitel_6/Lektion_8/mediathek/cd.php
Executable file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (count($_POST) > 0) {
|
||||
|
||||
// TODO: Lied in die Datenbank speichern
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($_GET['id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
$cdStatement = $db->prepare("
|
||||
SELECT
|
||||
cds.id,
|
||||
cds.name AS cdname,
|
||||
cds.erscheinungsjahr,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM cds
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
WHERE cds.id = :cd_id
|
||||
");
|
||||
$cdStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$cd = $cdStatement->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$liederStatement = $db->prepare("SELECT * FROM lieder where cd_id = :cd_id");
|
||||
$liederStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$lieder = $liederStatement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>
|
||||
<?= htmlspecialchars($cd['cdname']) ?>
|
||||
(<?= htmlspecialchars($cd['kuenstlername']) ?>, <?= htmlspecialchars($cd['erscheinungsjahr']) ?>)
|
||||
</h1>
|
||||
|
||||
<form action="cd.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Track</th>
|
||||
<th>Titel</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
<?php foreach ($lieder as $lied): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($lied['track']) ?></td>
|
||||
<td><?= htmlspecialchars($lied['titel']) ?></td>
|
||||
<td><a href="lied_bearbeiten.php?id=<?= htmlspecialchars($lied['id']) ?>">Bearbeiten ...</a></td>
|
||||
<td><a href="lied_loeschen.php?id=<?= htmlspecialchars($lied['id']) ?>">Löschen ...</a></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<tr>
|
||||
<td><input type="text" name="track" placeholder="Track"/></td>
|
||||
<td><input type="text" name="titel" placeholder="Titel"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
<input type="submit" value="Hinzufügen"/>
|
||||
</form>
|
||||
|
||||
<p><a href="index.php">zur Übersicht</a></p>
|
||||
|
||||
</body>
|
||||
</html>
|
46
Kapitel_6/Lektion_8/mediathek/index.php
Executable file
46
Kapitel_6/Lektion_8/mediathek/index.php
Executable file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$db = holeDatenbankverbindung();
|
||||
$statement = $db->query("
|
||||
SELECT
|
||||
cds.id,
|
||||
cds.name AS cdname,
|
||||
cds.erscheinungsjahr,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM cds
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
");
|
||||
$cds = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Künstler</th>
|
||||
<th>Album</th>
|
||||
<th>Erscheinungsjahr</th>
|
||||
</tr>
|
||||
<?php foreach ($cds as $cd): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($cd['kuenstlername']) ?></td>
|
||||
<td>
|
||||
<a href="cd.php?id=<?= $cd['id'] ?>">
|
||||
<?= htmlspecialchars($cd['cdname']) ?>
|
||||
</a>
|
||||
</td>
|
||||
<td><?= htmlspecialchars($cd['erscheinungsjahr']) ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
57
Kapitel_6/Lektion_8/mediathek/lied_bearbeiten.php
Executable file
57
Kapitel_6/Lektion_8/mediathek/lied_bearbeiten.php
Executable file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie bearbeiten möchten.';
|
||||
}
|
||||
|
||||
// TODO: Lied mit Angaben zu CD und Künstler aus der Datenbank laden
|
||||
$lied = [];
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion']) && $_POST['aktion'] === 'speichern') {
|
||||
|
||||
// TODO: Lied in der Datenbank aktualisieren
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php if (count($meldungen) > 0): ?>
|
||||
<p>Fehler:</p>
|
||||
<ul>
|
||||
<?php foreach ($meldungen as $meldung): ?>
|
||||
<li><?= htmlspecialchars($meldung) ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
<form action="lied_bearbeiten.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<h1>Lied bearbeiten</h1>
|
||||
<h2>'<?= htmlspecialchars($lied['cdname']) ?>' (<?= htmlspecialchars($lied['kuenstlername']) ?>)</h2>
|
||||
|
||||
<label>Track
|
||||
<input type="text" name="track" value="<?= htmlspecialchars($lied['track']) ?>"/>
|
||||
</label>
|
||||
<label>Titel
|
||||
<input type="text" name="titel" value="<?= htmlspecialchars($lied['titel']) ?>"/>
|
||||
</label>
|
||||
<button type="submit" name="aktion" value="speichern">Speichern</button>
|
||||
<button type="submit" name="aktion" value="abbrechen">Abbrechen</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
56
Kapitel_6/Lektion_8/mediathek/lied_loeschen.php
Executable file
56
Kapitel_6/Lektion_8/mediathek/lied_loeschen.php
Executable file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie löschen möchten.';
|
||||
}
|
||||
|
||||
// TODO: Lied aus der Datenbank laden (analog zu lied_bearbeiten.php)
|
||||
$lied = [];
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion']) && $_POST['aktion'] === 'loeschen') {
|
||||
|
||||
// TODO: Lied aus der Datenbank löschen
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php if (count($meldungen) > 0): ?>
|
||||
<p>Fehler:</p>
|
||||
<ul>
|
||||
<?php foreach ($meldungen as $meldung): ?>
|
||||
<li><?= htmlspecialchars($meldung) ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php else: ?>
|
||||
<form action="lied_loeschen.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<h1>Lied löschen</h1>
|
||||
<h2>'<?= htmlspecialchars($lied['cdname']) ?>' (<?= htmlspecialchars($lied['kuenstlername']) ?>)</h2>
|
||||
|
||||
<p>
|
||||
Möchten Sie Track <?= $lied['track'] ?> '<?= htmlspecialchars($lied['titel']) ?>' löschen?<br>
|
||||
Diese Aktion kann nicht rückgängig gemacht werden.
|
||||
</p>
|
||||
<button type="submit" name="aktion" value="loeschen">Löschen!</button>
|
||||
<button type="submit" name="aktion" value="abbrechen">Abbrechen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
</body>
|
||||
</html>
|
153
Kapitel_6/Lektion_8/mediathek/mediathek.sql
Executable file
153
Kapitel_6/Lektion_8/mediathek/mediathek.sql
Executable file
@ -0,0 +1,153 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 4.4.10
|
||||
-- http://www.phpmyadmin.net
|
||||
--
|
||||
-- Host: localhost:3306
|
||||
-- Erstellungszeit: 08. Aug 2016 um 18:32
|
||||
-- Server-Version: 5.5.42
|
||||
-- PHP-Version: 7.0.8
|
||||
|
||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8mb4 */;
|
||||
|
||||
--
|
||||
-- Datenbank: `mediathek`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `cds`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `cds`;
|
||||
CREATE TABLE `cds` (
|
||||
`id` int(11) NOT NULL,
|
||||
`kuenstler_id` int(11) NOT NULL,
|
||||
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`erscheinungsjahr` int(11) DEFAULT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `cds`
|
||||
--
|
||||
|
||||
INSERT INTO `cds` (`id`, `kuenstler_id`, `name`, `erscheinungsjahr`) VALUES
|
||||
(1, 1, 'Wallflower', 2015),
|
||||
(2, 2, 'Wo der Pfeffer wächst', 2004),
|
||||
(3, 1, 'The Girl In The Other Room', 2004);
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `kuenstler`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `kuenstler`;
|
||||
CREATE TABLE `kuenstler` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(32) COLLATE utf8_unicode_ci NOT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `kuenstler`
|
||||
--
|
||||
|
||||
INSERT INTO `kuenstler` (`id`, `name`) VALUES
|
||||
(1, 'Diana Krall'),
|
||||
(2, 'Wise Guys');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `lieder`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `lieder`;
|
||||
CREATE TABLE `lieder` (
|
||||
`id` int(11) NOT NULL,
|
||||
`cd_id` int(11) NOT NULL,
|
||||
`track` int(11) NOT NULL,
|
||||
`titel` varchar(64) COLLATE utf8_unicode_ci NOT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `lieder`
|
||||
--
|
||||
|
||||
INSERT INTO `lieder` (`id`, `cd_id`, `track`, `titel`) VALUES
|
||||
(1, 1, 1, 'California Dreamin'''),
|
||||
(2, 1, 2, 'Desperado'),
|
||||
(3, 1, 3, 'Superstar'),
|
||||
(4, 2, 1, 'Wo der Pfeffer wächst'),
|
||||
(5, 2, 2, 'Einer von den Wise Guys'),
|
||||
(6, 3, 1, 'Stop This World');
|
||||
|
||||
--
|
||||
-- Indizes der exportierten Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD KEY `kuenstler_id` (`kuenstler_id`);
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `kuenstler`
|
||||
--
|
||||
ALTER TABLE `kuenstler`
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD KEY `cd_id` (`cd_id`);
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT für exportierte Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `kuenstler`
|
||||
--
|
||||
ALTER TABLE `kuenstler`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
|
||||
--
|
||||
-- Constraints der exportierten Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
ADD CONSTRAINT `cds_ibfk_1` FOREIGN KEY (`kuenstler_id`) REFERENCES `kuenstler` (`id`);
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
ADD CONSTRAINT `lieder_ibfk_1` FOREIGN KEY (`cd_id`) REFERENCES `cds` (`id`);
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
34
Kapitel_6/Lektion_8/mediathek_loesung/_application.php
Executable file
34
Kapitel_6/Lektion_8/mediathek_loesung/_application.php
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @return PDO
|
||||
*/
|
||||
function holeDatenbankverbindung()
|
||||
{
|
||||
return new PDO(
|
||||
'mysql:dbname=mediathek;host=localhost',
|
||||
'root',
|
||||
'root',
|
||||
[PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"]
|
||||
);
|
||||
}
|
||||
|
||||
function holeLied($liedId)
|
||||
{
|
||||
$db = holeDatenbankverbindung();
|
||||
$liedStatement = $db->prepare("
|
||||
SELECT
|
||||
lieder.track,
|
||||
lieder.titel,
|
||||
cds.id AS cd_id,
|
||||
cds.name AS cdname,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM lieder
|
||||
LEFT JOIN cds ON lieder.cd_id = cds.id
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
WHERE lieder.id = :lied_id
|
||||
");
|
||||
$liedStatement->execute(['lied_id' => $liedId]);
|
||||
|
||||
return $liedStatement->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
77
Kapitel_6/Lektion_8/mediathek_loesung/cd.php
Executable file
77
Kapitel_6/Lektion_8/mediathek_loesung/cd.php
Executable file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (count($_POST) > 0) {
|
||||
$insertStatement = $db->prepare("INSERT INTO lieder (cd_id, track, titel) VALUES (:cd_id, :track, :titel)");
|
||||
$insertStatement->execute([
|
||||
'cd_id' => $_GET['id'],
|
||||
'track' => $_POST['track'],
|
||||
'titel' => $_POST['titel']
|
||||
]);
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($_GET['id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
$cdStatement = $db->prepare("
|
||||
SELECT
|
||||
cds.id,
|
||||
cds.name AS cdname,
|
||||
cds.erscheinungsjahr,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM cds
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
WHERE cds.id = :cd_id
|
||||
");
|
||||
$cdStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$cd = $cdStatement->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$liederStatement = $db->prepare("SELECT * FROM lieder where cd_id = :cd_id ORDER BY track ASC");
|
||||
$liederStatement->execute(['cd_id' => $_GET['id']]);
|
||||
$lieder = $liederStatement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>
|
||||
<?= htmlspecialchars($cd['cdname']) ?>
|
||||
(<?= htmlspecialchars($cd['kuenstlername']) ?>, <?= htmlspecialchars($cd['erscheinungsjahr']) ?>)
|
||||
</h1>
|
||||
|
||||
<form action="cd.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Track</th>
|
||||
<th>Titel</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
<?php foreach ($lieder as $lied): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($lied['track']) ?></td>
|
||||
<td><?= htmlspecialchars($lied['titel']) ?></td>
|
||||
<td><a href="lied_bearbeiten.php?id=<?= htmlspecialchars($lied['id']) ?>">Bearbeiten ...</a></td>
|
||||
<td><a href="lied_loeschen.php?id=<?= htmlspecialchars($lied['id']) ?>">Löschen ...</a></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<tr>
|
||||
<td><input type="text" name="track" placeholder="Track"/></td>
|
||||
<td><input type="text" name="titel" placeholder="Titel"/></td>
|
||||
</tr>
|
||||
</table>
|
||||
<input type="submit" value="Hinzufügen"/>
|
||||
</form>
|
||||
|
||||
<p><a href="index.php">zur Übersicht</a></p>
|
||||
|
||||
</body>
|
||||
</html>
|
46
Kapitel_6/Lektion_8/mediathek_loesung/index.php
Executable file
46
Kapitel_6/Lektion_8/mediathek_loesung/index.php
Executable file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$db = holeDatenbankverbindung();
|
||||
$statement = $db->query("
|
||||
SELECT
|
||||
cds.id,
|
||||
cds.name AS cdname,
|
||||
cds.erscheinungsjahr,
|
||||
kuenstler.name AS kuenstlername
|
||||
FROM cds
|
||||
LEFT JOIN kuenstler ON cds.kuenstler_id = kuenstler.id
|
||||
");
|
||||
$cds = $statement->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Künstler</th>
|
||||
<th>Album</th>
|
||||
<th>Erscheinungsjahr</th>
|
||||
</tr>
|
||||
<?php foreach ($cds as $cd): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($cd['kuenstlername']) ?></td>
|
||||
<td>
|
||||
<a href="cd.php?id=<?= $cd['id'] ?>">
|
||||
<?= htmlspecialchars($cd['cdname']) ?>
|
||||
</a>
|
||||
</td>
|
||||
<td><?= htmlspecialchars($cd['erscheinungsjahr']) ?></td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
63
Kapitel_6/Lektion_8/mediathek_loesung/lied_bearbeiten.php
Executable file
63
Kapitel_6/Lektion_8/mediathek_loesung/lied_bearbeiten.php
Executable file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie bearbeiten möchten.';
|
||||
}
|
||||
|
||||
$lied = holeLied($_GET['id']);
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion'])) {
|
||||
if ($_POST['aktion'] === 'speichern') {
|
||||
$updateStatement = $db->prepare("UPDATE lieder SET track = :track, titel = :titel WHERE id = :lied_id");
|
||||
$updateStatement->execute([
|
||||
'track' => $_POST['track'],
|
||||
'titel' => $_POST['titel'],
|
||||
'lied_id' => $_GET['id']
|
||||
]);
|
||||
}
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php if (count($meldungen) > 0): ?>
|
||||
<p>Fehler:</p>
|
||||
<ul>
|
||||
<?php foreach ($meldungen as $meldung): ?>
|
||||
<li><?= htmlspecialchars($meldung) ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php endif; ?>
|
||||
<form action="lied_bearbeiten.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<h1>Lied bearbeiten</h1>
|
||||
<h2>'<?= htmlspecialchars($lied['cdname']) ?>' (<?= htmlspecialchars($lied['kuenstlername']) ?>)</h2>
|
||||
|
||||
<label>Track
|
||||
<input type="text" name="track" value="<?= htmlspecialchars($lied['track']) ?>"/>
|
||||
</label>
|
||||
<label>Titel
|
||||
<input type="text" name="titel" value="<?= htmlspecialchars($lied['titel']) ?>"/>
|
||||
</label>
|
||||
<button type="submit" name="aktion" value="speichern">Speichern</button>
|
||||
<button type="submit" name="aktion" value="abbrechen">Abbrechen</button>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
58
Kapitel_6/Lektion_8/mediathek_loesung/lied_loeschen.php
Executable file
58
Kapitel_6/Lektion_8/mediathek_loesung/lied_loeschen.php
Executable file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/_application.php';
|
||||
|
||||
$meldungen = [];
|
||||
$db = holeDatenbankverbindung();
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
$meldungen[] = 'Bitte geben Sie die ID des Liedes an, das Sie löschen möchten.';
|
||||
}
|
||||
|
||||
$lied = holeLied($_GET['id']);
|
||||
if (!$lied) {
|
||||
$meldungen[] = 'Es konnte kein Lied mit der ID ' . htmlspecialchars($_GET['id']) . ' gefunden werden.';
|
||||
}
|
||||
|
||||
if (isset($_POST['aktion'])) {
|
||||
if ($_POST['aktion'] === 'loeschen') {
|
||||
$deleteStatement = $db->prepare("DELETE FROM lieder WHERE id = :lied_id");
|
||||
$deleteStatement->execute(['lied_id' => $_GET['id']]);
|
||||
}
|
||||
|
||||
header('Location: cd.php?id=' . htmlspecialchars($lied['cd_id']));
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Mediathek</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<?php if (count($meldungen) > 0): ?>
|
||||
<p>Fehler:</p>
|
||||
<ul>
|
||||
<?php foreach ($meldungen as $meldung): ?>
|
||||
<li><?= htmlspecialchars($meldung) ?></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php else: ?>
|
||||
<form action="lied_loeschen.php?id=<?= htmlspecialchars($_GET['id']) ?>" method="post">
|
||||
<h1>Lied löschen</h1>
|
||||
<h2>'<?= htmlspecialchars($lied['cdname']) ?>' (<?= htmlspecialchars($lied['kuenstlername']) ?>)</h2>
|
||||
|
||||
<p>
|
||||
Möchten Sie Track <?= $lied['track'] ?> '<?= htmlspecialchars($lied['titel']) ?>' löschen?<br>
|
||||
Diese Aktion kann nicht rückgängig gemacht werden.
|
||||
</p>
|
||||
<button type="submit" name="aktion" value="loeschen">Löschen!</button>
|
||||
<button type="submit" name="aktion" value="abbrechen">Abbrechen</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
</body>
|
||||
</html>
|
153
Kapitel_6/Lektion_8/mediathek_loesung/mediathek.sql
Executable file
153
Kapitel_6/Lektion_8/mediathek_loesung/mediathek.sql
Executable file
@ -0,0 +1,153 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 4.4.10
|
||||
-- http://www.phpmyadmin.net
|
||||
--
|
||||
-- Host: localhost:3306
|
||||
-- Erstellungszeit: 08. Aug 2016 um 18:32
|
||||
-- Server-Version: 5.5.42
|
||||
-- PHP-Version: 7.0.8
|
||||
|
||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
|
||||
/*!40101 SET NAMES utf8mb4 */;
|
||||
|
||||
--
|
||||
-- Datenbank: `mediathek`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `cds`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `cds`;
|
||||
CREATE TABLE `cds` (
|
||||
`id` int(11) NOT NULL,
|
||||
`kuenstler_id` int(11) NOT NULL,
|
||||
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
|
||||
`erscheinungsjahr` int(11) DEFAULT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `cds`
|
||||
--
|
||||
|
||||
INSERT INTO `cds` (`id`, `kuenstler_id`, `name`, `erscheinungsjahr`) VALUES
|
||||
(1, 1, 'Wallflower', 2015),
|
||||
(2, 2, 'Wo der Pfeffer wächst', 2004),
|
||||
(3, 1, 'The Girl In The Other Room', 2004);
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `kuenstler`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `kuenstler`;
|
||||
CREATE TABLE `kuenstler` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(32) COLLATE utf8_unicode_ci NOT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `kuenstler`
|
||||
--
|
||||
|
||||
INSERT INTO `kuenstler` (`id`, `name`) VALUES
|
||||
(1, 'Diana Krall'),
|
||||
(2, 'Wise Guys');
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `lieder`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `lieder`;
|
||||
CREATE TABLE `lieder` (
|
||||
`id` int(11) NOT NULL,
|
||||
`cd_id` int(11) NOT NULL,
|
||||
`track` int(11) NOT NULL,
|
||||
`titel` varchar(64) COLLATE utf8_unicode_ci NOT NULL
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||
|
||||
--
|
||||
-- Daten für Tabelle `lieder`
|
||||
--
|
||||
|
||||
INSERT INTO `lieder` (`id`, `cd_id`, `track`, `titel`) VALUES
|
||||
(1, 1, 1, 'California Dreamin'''),
|
||||
(2, 1, 2, 'Desperado'),
|
||||
(3, 1, 3, 'Superstar'),
|
||||
(4, 2, 1, 'Wo der Pfeffer wächst'),
|
||||
(5, 2, 2, 'Einer von den Wise Guys'),
|
||||
(6, 3, 1, 'Stop This World');
|
||||
|
||||
--
|
||||
-- Indizes der exportierten Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD KEY `kuenstler_id` (`kuenstler_id`);
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `kuenstler`
|
||||
--
|
||||
ALTER TABLE `kuenstler`
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
--
|
||||
-- Indizes für die Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
ADD PRIMARY KEY (`id`),
|
||||
ADD KEY `cd_id` (`cd_id`);
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT für exportierte Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=4;
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `kuenstler`
|
||||
--
|
||||
ALTER TABLE `kuenstler`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
|
||||
--
|
||||
-- AUTO_INCREMENT für Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=7;
|
||||
--
|
||||
-- Constraints der exportierten Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `cds`
|
||||
--
|
||||
ALTER TABLE `cds`
|
||||
ADD CONSTRAINT `cds_ibfk_1` FOREIGN KEY (`kuenstler_id`) REFERENCES `kuenstler` (`id`);
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `lieder`
|
||||
--
|
||||
ALTER TABLE `lieder`
|
||||
ADD CONSTRAINT `lieder_ibfk_1` FOREIGN KEY (`cd_id`) REFERENCES `cds` (`id`);
|
||||
|
||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
Loading…
Reference in New Issue
Block a user