Update
This commit is contained in:
79
cgi-bin/uebung09/Database.php
Normal file
79
cgi-bin/uebung09/Database.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getQuestions()
|
||||||
|
{
|
||||||
|
$result = $this->connection->query("SELECT * FROM questions");
|
||||||
|
|
||||||
|
$resultArray = [];
|
||||||
|
|
||||||
|
while ($line = $result->fetch_assoc()) {
|
||||||
|
array_push($resultArray, $line);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result->free();
|
||||||
|
|
||||||
|
return $resultArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
38
cgi-bin/uebung09/questions-show.php
Normal file
38
cgi-bin/uebung09/questions-show.php
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once(__DIR__ . 'Database.php');
|
||||||
|
|
||||||
|
echo <<<END
|
||||||
|
<h1>Fragen anzeigen</h1>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<td>Frage</td>
|
||||||
|
<td>Antwort0</td>
|
||||||
|
<td>Antwort1</td>
|
||||||
|
<td>Antwort2</td>
|
||||||
|
<td></td>
|
||||||
|
<td></td>
|
||||||
|
</tr>
|
||||||
|
END;
|
||||||
|
|
||||||
|
$db = new Database();
|
||||||
|
|
||||||
|
$questions = $db->getQuestions();
|
||||||
|
|
||||||
|
foreach($questions as $question) {
|
||||||
|
echo("<tr>");
|
||||||
|
echo("<td>$question->question</td>");
|
||||||
|
echo("<td>$question->answer0</td>");
|
||||||
|
echo("<td>$question->answer1</td>");
|
||||||
|
echo("<td>$question->answer2</td>");
|
||||||
|
echo("<td>$question->solution</td>");
|
||||||
|
echo("</tr>");
|
||||||
|
}
|
||||||
|
|
||||||
|
echo <<<END
|
||||||
|
</table>
|
||||||
|
<br><br>
|
||||||
|
<a href="questions-create.php">Frage hinzufügen</a>
|
||||||
|
END;
|
||||||
|
|
||||||
|
?>
|
Reference in New Issue
Block a user