Update
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Database
|
||||
{
|
||||
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* Database constructor.
|
||||
*
|
||||
* Baut die Verbindung zur Datenbank auf
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// MySQL-Zugangsdaten
|
||||
// Hier: Automatisch auslesen aus .my.cnf. Sonst einfach von Hand eintragen
|
||||
$user = get_current_user(); // Benutzer, dem diese Datei gehört!
|
||||
$myCnf = parse_ini_file("/home/$user/.my.cnf");
|
||||
|
||||
$host = $myCnf['host'];
|
||||
$user = $myCnf['user'];
|
||||
$password = $myCnf['password'];
|
||||
$database = $myCnf['database'];
|
||||
|
||||
$this->connection = new mysqli($host, $user, $password, $database);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schließt die Verbindung zru Datenbank
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->connection->close();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Löscht die Tabelle tasks und erstelle sie anschließend erneut
|
||||
*/
|
||||
public function reset()
|
||||
{
|
||||
$this->connection->query("DROP TABLE IF EXISTS tasks");
|
||||
$this->connection->query("CREATE TABLE tasks(id INT NOT NULL AUTO_INCREMENT, name TEXT, PRIMARY KEY (id));");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fügt einen Task mit dem Namen $name in die Tabelle tasks ein
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool true, falls Einfügen erfolgreich
|
||||
*/
|
||||
public function addTask($name)
|
||||
{
|
||||
$statement = $this->connection->prepare("INSERT INTO tasks(name) VALUES(?)");
|
||||
$statement->bind_param("s", $name);
|
||||
return $statement->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht den Taks mit der ID $id aus der Tabelle tasks
|
||||
*
|
||||
* @param int $id
|
||||
* @return bool, falls Löschen erfolgreich
|
||||
*/
|
||||
public function deleteTask($id)
|
||||
{
|
||||
$statement = $this->connection->prepare("DELETE FROM tasks WHERE id = ?");
|
||||
$statement->bind_param("i", $id);
|
||||
return $statement->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Liefert ein assozaitves Array aller in der Tabelle tasks gespeicherten Einträge
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTasks()
|
||||
{
|
||||
$result = $this->connection->query("SELECT * FROM tasks");
|
||||
|
||||
$resultArray = [];
|
||||
|
||||
while ($line = $result->fetch_assoc()) {
|
||||
array_push($resultArray, $line);
|
||||
}
|
||||
|
||||
$result->free();
|
||||
|
||||
return $resultArray;
|
||||
}
|
||||
|
||||
|
||||
}
|
57
uebung9/uebung09-examples/to-do-list-database/index.php
Normal file
57
uebung9/uebung09-examples/to-do-list-database/index.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Meine Todo-Liste</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Todo-Liste</h1>
|
||||
|
||||
|
||||
<?php
|
||||
|
||||
require_once(__DIR__ . "/inc/Database.php");
|
||||
|
||||
$db = new Database();
|
||||
|
||||
$tasks = $db->getTasks();
|
||||
|
||||
if (sizeof($tasks) > 0) {
|
||||
echo "<ul>";
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
|
||||
$name = $task['name'];
|
||||
$id = $task['id'];
|
||||
|
||||
|
||||
echo "<li>$name <form action='tasks-delete.php' method='post' class='inline-form'><button name='id' value='$id'>löschen</button></form> </li>";
|
||||
}
|
||||
|
||||
echo "</ul>";
|
||||
} else {
|
||||
echo "Nichts zu tun";
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
<form action="tasks-store.php" method="post">
|
||||
|
||||
<label for="task">Todo:</label>
|
||||
<input name="name" type="text" id="task">
|
||||
|
||||
<button type="submit">Hinzufügen</button>
|
||||
|
||||
</form>
|
||||
|
||||
<p><a href="reset.php">Liste zurücksetzen</a></p>
|
||||
|
||||
|
||||
</body>
|
||||
</html>
|
7
uebung9/uebung09-examples/to-do-list-database/reset.php
Normal file
7
uebung9/uebung09-examples/to-do-list-database/reset.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
require_once(__DIR__ . "/inc/Database.php");
|
||||
|
||||
$db = new Database();
|
||||
$db->reset();
|
||||
|
||||
header("Location: index.php");
|
24
uebung9/uebung09-examples/to-do-list-database/style.css
Normal file
24
uebung9/uebung09-examples/to-do-list-database/style.css
Normal file
@@ -0,0 +1,24 @@
|
||||
body {
|
||||
font-family: sans-serif;
|
||||
text-align: center;
|
||||
background-color: #eeeeee;
|
||||
}
|
||||
|
||||
.inline-form {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
.inline-form button {
|
||||
border: 0;
|
||||
background-color: #c6c8ca;
|
||||
border-radius: 1em;
|
||||
font-size: x-small;
|
||||
padding: 0.3em;
|
||||
}
|
||||
|
||||
.inline-form button:hover {
|
||||
background-color: red;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
require_once(__DIR__ . "/inc/Database.php");
|
||||
|
||||
$id = $_POST['id'];
|
||||
|
||||
$db = new Database();
|
||||
$db->deleteTask($id);
|
||||
|
||||
header("Location: index.php");
|
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
require_once(__DIR__ . "/inc/Database.php");
|
||||
|
||||
$name = $_POST['name'];
|
||||
|
||||
// Nur nicht leere Tasks sollen gespeichert werden
|
||||
if (!empty($name) && !empty(trim($name))) {
|
||||
|
||||
$db = new Database();
|
||||
$db->addTask($name);
|
||||
}
|
||||
|
||||
|
||||
header("Location: index.php");
|
Reference in New Issue
Block a user