Update
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
// ----------------------------------------------------
|
||||
// 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'];
|
||||
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Prepared Statement für alles außer SELECT
|
||||
// ----------------------------------------------------
|
||||
|
||||
// Variablen, die wir einfügen wollen
|
||||
// Diese kommen normalerweise aus einem POST-Request
|
||||
$string = "Ein String";
|
||||
$integer = 12345;
|
||||
|
||||
// Verbindung aufbauen
|
||||
$mysqli = new mysqli($host, $user, $password, $database);
|
||||
|
||||
// Prepare: Für jede Variable ein ?
|
||||
$statement = $mysqli->prepare("INSERT INTO t VALUES(?, ?)");
|
||||
|
||||
// Parameter binden: Für jedes ? Typ (s=String, i=Integer) und Variable angeben
|
||||
$statement->bind_param("si", $string, $integer);
|
||||
|
||||
// Query ausführen
|
||||
$statement->execute();
|
||||
|
||||
// Statement schließen
|
||||
$statement->close();
|
||||
|
||||
// Verbindung schließen
|
||||
$mysqli->close();
|
||||
|
||||
|
||||
// ----------------------------------------------------
|
||||
// Prepared Statement bei SELECT-Queries
|
||||
// ----------------------------------------------------
|
||||
|
||||
// Variablen, die wir in der Query verwenden wollen
|
||||
// Diese kommt normalerweise aus einem GET- oder POST-Request
|
||||
// Das %-Zeichen ist ein Platzhalter und bedeutet "hier darf irgendwas stehen"
|
||||
$search_string = "%String%";
|
||||
|
||||
|
||||
// Verbindung aufbauen
|
||||
$mysqli = new mysqli($host, $user, $password, $database);
|
||||
|
||||
// Prepare: Für jede Variable ein ?
|
||||
$statement = $mysqli->prepare("SELECT * FROM t WHERE string_column LIKE ?");
|
||||
|
||||
// Parameter binden: Für jedes ? Typ (s=String, i=Integer) und Variable angeben
|
||||
$statement->bind_param("s", $search_string);
|
||||
|
||||
// Query ausführen
|
||||
$statement->execute();
|
||||
|
||||
// Ergebnis an variablen binden: Für jede Spalte aus dem Result-Set eine Variable
|
||||
// Hier zwei Spalten => zwei Variablen!
|
||||
$statement->bind_result($string_column, $integer_column);
|
||||
|
||||
// Über das Result-Set iterieren (fetch_assoc() geht hier NICHT!)
|
||||
while ($statement->fetch()) {
|
||||
// Die Variablen aus bind_result werden jetzt mit Werten aus dem Result-Set gefüllt!
|
||||
echo "string_column hat den Wert: $string_column. ";
|
||||
echo "integer_column hat den Wert: $integer_column. ";
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
|
||||
// Speicher des Result-Sets freigeben
|
||||
$statement->free_result();
|
||||
|
||||
// Statement schließen (Speicher des Statements freigeben)
|
||||
$statement->close();
|
||||
|
||||
// Verbindung schließen
|
||||
$mysqli->close();
|
13
uebung9/uebung09-examples/prepared-statements/setup.sql
Normal file
13
uebung9/uebung09-examples/prepared-statements/setup.sql
Normal file
@@ -0,0 +1,13 @@
|
||||
DROP TABLE IF EXISTS t;
|
||||
|
||||
CREATE TABLE t (
|
||||
string_column TEXT NOT NULL,
|
||||
integer_colum INT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO t
|
||||
VALUES
|
||||
("Erster String", 98765),
|
||||
("Zweiter String", 87654),
|
||||
("Dritter String", 76543);
|
||||
|
@@ -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