eingabe+sep files

master
Helge 2022-01-09 23:50:09 +01:00
parent ccee54ee09
commit 314885692a
2 changed files with 94 additions and 73 deletions

View File

@ -1,83 +1,28 @@
<html>
<head>
</head>
<main>
<form action="index.php" method="post">
Name: <input type="text" name="linkName"> URL: <input type="text" name="linkUrl"> <input type="Submit" value="Speichern">
</form>
</main>
</html>
<?php
include ('link.php');
class Link{
public $name;
public $url;
public function __construct($newName, $newUrl){
$this->name = $newName;
$this->url = $newUrl;
}
}
class dbLinkHandler
{
private $pdo;
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
$this->pdo = new PDO('mysql:host=localhost;dbname=linkdb', 'linkdb', 'linkdb');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_id BIGINT(18) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link_url VARCHAR(30) NOT NULL,
link_name VARCHAR(30),
reg_date TIMESTAMP
)';
$this->pdo->exec($sql); // use exec() because no results are returned
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
public function saveLink($link)
{
$statement = $this->pdo->prepare('INSERT INTO link(link_url, link_name) VALUES (:link_url, :link_name)');
$statement->execute
([
':link_url' => $link->url,
':link_name' => $link->name
]);
}
public function getListReverse()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link ORDER BY link_id DESC";
return $this->pdo->query($sql);
}
public function getList()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link";
return $this->pdo->query($sql);
}
}
// Main
$db = new dbLinkHAndler();
$link1=new Link("cloud.therealblue.de", "https://cloud.therealblue.de");
if ($_POST["linkName"] != "" AND $_POST["linkUrl"] != "")
{
$link1=new Link($_POST["linkName"], $_POST["linkUrl"]);
$db->saveLink($link1);
}
foreach ($db->getListReverse() as $link)
{
echo $link['link_id'].': <a href="'.$link['link_url'].'" target="_blank">'.$link['link_name'].'</a><br>';
}
?>

76
link.php 100644
View File

@ -0,0 +1,76 @@
<?php
class Link{
public $name;
public $url;
public function __construct($newName, $newUrl){
$this->name = $newName;
$this->url = $newUrl;
}
}
class dbLinkHandler
{
private $pdo;
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
$this->pdo = new PDO('mysql:host=localhost;dbname=linkdb', 'linkdb', 'linkdb');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_id BIGINT(18) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link_url VARCHAR(50) NOT NULL,
link_name VARCHAR(100),
reg_date TIMESTAMP
)';
$this->pdo->exec($sql); // use exec() because no results are returned
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
public function saveLink($link)
{
try
{
$statement = $this->pdo->prepare('INSERT INTO link(link_url, link_name) VALUES (:link_url, :link_name)');
$statement->execute
([
':link_url' => $link->url,
':link_name' => $link->name
]);
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
public function getListReverse()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link ORDER BY link_id DESC";
return $this->pdo->query($sql);
}
public function getList()
{
// Mit verschachtelten Arrays optimieren!! Array schlüssel id enthält array mit schlüssel link_id, link_name, etc (repräsentiert datensatz)
$list = array();
$sql = "SELECT * FROM link";
return $this->pdo->query($sql);
}
}
?>