From c949aebc85e1b56c43a57dc9b2f0d08a324f7c5f Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sat, 26 May 2018 15:09:18 +0200 Subject: [PATCH] create database connection --- php/generateLink.php | 29 ++++++++++++++++++++++++++++- sql/createDatabase.sql | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 sql/createDatabase.sql diff --git a/php/generateLink.php b/php/generateLink.php index 321868f..e9d0332 100644 --- a/php/generateLink.php +++ b/php/generateLink.php @@ -2,10 +2,37 @@ $uniqueID = uniqid(); $resultStr = "Neuer eintrag über: http://localhost/github/ipr-projekt/entry.php?tan="; - $resultStr = $resultStr . uniqid(); + $resultStr = $resultStr . $uniqueID; + insertTan($uniqueID); $HTMLJSON = array('html' => $resultStr); echo json_encode($HTMLJSON); + function insertTan($tan) { + //Stelle DB verbindung her + $user = 'root'; + $password = 'root'; + $db = 'friendsbook'; + $host = 'localhost'; + $port = 3306; + + $database = new mysqli($host, $user, $password, $db, $port); + + if ($database->connect_error) { + die('Connect Error (' . $database->connect_errno . ') ' . $database->connect_error); + } + + $stmt = $database->prepare("INSERT INTO tans (tan, used) VALUES (?, ?)"); + + $false = false; + $stmt->bind_param("si", $tan, $false); + + try { + $stmt->execute(); + } catch (PDOException $e) { + $e->getMessage(); + } + } + ?> diff --git a/sql/createDatabase.sql b/sql/createDatabase.sql new file mode 100644 index 0000000..2904e4c --- /dev/null +++ b/sql/createDatabase.sql @@ -0,0 +1 @@ +CREATE TABLE `friendsbook`.`tans` ( `id` INT NOT NULL AUTO_INCREMENT , `tan` VARCHAR(13) NOT NULL , `used` BOOLEAN NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;