PHP-databaskonstruktion

Log | Files | Refs

commit 9e29803aaa0e0a3ce0cf68ca4e87d4ef02ce818c
parent ad645f693d6363e37724a18a0b3b7a5daf1cebb4
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Thu, 28 Sep 2023 16:04:42 +0000

Added procedureHandler class.

Diffstat:
Mdb/dbhelper.php | 28----------------------------
Mincident.php | 4+++-
Mutils/imports.php | 1+
Mutils/logs.txt | 16++++++++++++++++
Autils/procedureHandler.php | 40++++++++++++++++++++++++++++++++++++++++
5 files changed, 60 insertions(+), 29 deletions(-)

diff --git a/db/dbhelper.php b/db/dbhelper.php @@ -61,32 +61,4 @@ header("Location: " . $location); exit(); } - - function HandleProcedure($postData) { - if (isset($postData['OperationType']) && $postData['OperationType'] == 'EXECUTE') { - - try { - $db = dbconnection::getInstance(); - $pdo = $db->getPdo(); - - if (isset($postData['Function'])) { - $procedureName = $postData['Function']; - - $dateCreated = $postData['DateCreated']; - $title = $postData['Title']; - - $sql = "CALL {$procedureName}(:dateCreated, :title)"; - $stmt = $pdo->prepare($sql); - $stmt->bindParam(':dateCreated', $dateCreated); - $stmt->bindParam(':title', $title); - - $stmt->execute(); - } - } catch(PDOException $e) { - echo "Error: " . $e->getMessage(); - } - - RefreshTables(); - } - } ?> \ No newline at end of file diff --git a/incident.php b/incident.php @@ -2,6 +2,7 @@ require 'utils/imports.php'; dbconnection::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); $handlerFactory = new InsertHandlerFactory(); + $procedureHandler = new ProcedureHandler(); $incidentName = urldecode($_GET['IncidentName']); $incidentNumber = urldecode($_GET['IncidentNumber']); @@ -49,7 +50,8 @@ </form>"; } - HandleProcedure($_POST); + //Run archive report procedure + $procedureHandler->handlePostData($_POST); $WriteReportModal = (new ModalBuilder()) ->setModalId('insertReport') diff --git a/utils/imports.php b/utils/imports.php @@ -8,4 +8,5 @@ include 'postHandlerInterface.php'; include 'insertHandler.php'; include 'updateHandler.php'; + include 'procedureHandler.php'; ?> \ No newline at end of file diff --git a/utils/logs.txt b/utils/logs.txt @@ -760,3 +760,19 @@ 2023-09-28 15:45:03 - Incident: 'asdasdasd', '12312323' 2023-09-28 15:45:11 - Incident: 'asdasdasd', '12312323' 2023-09-28 15:45:12 - Incident: 'asdasdasd', '12312323' +2023-09-28 15:49:11 - Incident: 'Arson', '108' +2023-09-28 15:49:14 - Incident: 'Arson', '108' +2023-09-28 15:49:14 - Incident: 'Arson', '108' +2023-09-28 15:49:46 - Incident: 'inringning', '901' +2023-09-28 15:49:49 - Incident: 'inringning', '901' +2023-09-28 15:49:49 - Incident: 'inringning', '901' +2023-09-28 15:58:52 - Incident: 'Ambush', '106' +2023-09-28 15:58:55 - Incident: 'Espionage', '104' +2023-09-28 15:59:27 - Incident: 'Espionage', '104' +2023-09-28 15:59:27 - Inserting into: Report +2023-09-28 15:59:27 - Incident: 'Espionage', '104' +2023-09-28 15:59:30 - Incident: 'Espionage', '104' +2023-09-28 16:03:06 - Incident: 'Espionage', '104' +2023-09-28 16:03:07 - Incident: 'Espionage', '104' +2023-09-28 16:03:09 - Incident: 'Espionage', '104' +2023-09-28 16:03:09 - Incident: 'Espionage', '104' diff --git a/utils/procedureHandler.php b/utils/procedureHandler.php @@ -0,0 +1,39 @@ +<?php + class ProcedureHandler implements PostHandler { + function handlePostData($postData) { + if (isset($postData['OperationType']) && $postData['OperationType'] == 'EXECUTE') { + try { + $db = dbconnection::getInstance(); + $pdo = $db->getPdo(); + + if (isset($postData['Function'])) { + + $procedureName = $postData['Function']; + unset($postData['Function'], $postData['OperationType']); + + $placeholders = array_keys($postData); + $sql = "CALL {$procedureName}(" . implode(", ", array_map(function($p) { + return ":$p"; + }, $placeholders)) . ")"; + + $stmt = $pdo->prepare($sql); + + foreach ($postData as $key => $value) { + $stmt->bindParam(":$key", $postData[$key]); + } + + $stmt->execute(); + } + } catch(PDOException $e) { + echo "Error: " . $e->getMessage(); + } + + RefreshTables(); + } + } + + public function getOperationType() { + return "INSERT"; + } + } +?> +\ No newline at end of file