link sete

master
Helge 2022-01-13 00:22:32 +01:00
parent 64dd4352d0
commit 1c6098e9a8
2 changed files with 108 additions and 78 deletions

View File

@ -1,82 +1,17 @@
<?php
class Link{
public $name;
public $url;
include ('link_lib.php');
public function __construct($newName, $newUrl){
$this->name = $newName;
$this->url = $newUrl;
}
}
$db = new dbLinkHAndler();
class dbLinkHandler
{
private $pdo;
$link = $db->getLink(filter_var($_GET["url"], FILTER_SANITIZE_STRING));
?>
public function __construct()
{
try
{
// Verbindung zur Datenbank herstellen
$this->pdo = new PDO('mysql:host=localhost;dbname=linkvz', 'linkvz', 'linkvz');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_url VARCHAR(50) NOT NULL PRIMARY KEY,
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)
{
if ($e->errorInfo[1] == 1062)
{
echo 'haaaaaa<br><br>';
}
else
{
echo $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_name DESC";
return $this->pdo->query($sql);
}
public function getList()
{
//$sql = "SELECT * FROM link";
$statement = $this->pdo->prepare("SELECT * FROM link");
$statement->execute();
//return $this->pdo->query($sql);
return $statement->fetch;
}
}
?>
<html>
<head>
</head>
<body>
<p>Name: <?= $link->name; ?></p>
<p>URL: <a href="<?= $link->url; ?>" target="_blank"><?= $link->url; ?></a></p>
<p>Hinzugefügt: <?= $link->reg_date; ?></p>
</body>
</html>

95
link_lib.php 100644
View File

@ -0,0 +1,95 @@
<?php
class Link{
public $name;
public $url;
public $reg_date;
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=linkvz', 'linkvz', 'linkvz');
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Tabelle link erstellen, falls nicht vorhanden
$sql = 'CREATE TABLE IF NOT EXISTS link (
link_url VARCHAR(100) NOT NULL PRIMARY KEY,
link_name VARCHAR(50),
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)
{
if ($e->errorInfo[1] == 1062)
{
echo "haaaaaa!<br>";
}
else
{
echo $e->getMessage();
}
}
}
public function getListReverse()
{
$statement = $this->pdo->prepare("SELECT * FROM link ORDER BY reg_date DESC");
$statement->execute();
return $statement;
}
public function getList()
{
$statement = $this->pdo->prepare("SELECT * FROM link");
$statement->execute();
return $statement;
}
public function getLink($url)
{
$statement = $this->pdo->prepare("SELECT * FROM link WHERE link_url = ?");
$statement->execute(array($url));
$fetch = $statement->fetch();
$link = new link($fetch[1], $fetch[0]);
$link->reg_date = $fetch[2];
return $link;
}
}
?>