PHP-databaskonstruktion

Log | Files | Refs

commit 11f6e4e0f98f62b9b8e255ade2ccb28b9f4a545a
parent a1d17a738a7b57cb5107a9a54d07fbfc7dbfbf05
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Fri, 22 Sep 2023 16:20:39 +0000

Changed to workersworld database.

Diffstat:
MComponents.php | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
ADatabase.php | 37+++++++++++++++++++++++++++++++++++++
DIncidents.php | 24------------------------
DTerrain.php | 25-------------------------
Aincidents.php | 30++++++++++++++++++++++++++++++
Mindex.php | 11+++++++----
Moperations.php | 11+++++++----
Aterrain.php | 28++++++++++++++++++++++++++++
8 files changed, 203 insertions(+), 92 deletions(-)

diff --git a/Components.php b/Components.php @@ -1,6 +1,7 @@ <?php function displayTable($tableName) { - $pdo = new PDO('mysql:dbname=a22willi;host=localhost;', "root", ''); + $db = Database::getInstance(); + $pdo = $db->getPdo(); // Start the table $output = "<table class='table table-striped table-bordered'>"; @@ -90,53 +91,111 @@ "; } - function generateInsertFunction($tableName) { - $pdo = new PDO('mysql:dbname=a22willi;host=localhost;', 'root', ''); + function hasInsertPrivilege($tableName) { + // Get the PDO instance from the Database singleton + $db = Database::getInstance(); + $pdo = $db->getPdo(); - // Fetch column names - $stmt = $pdo->prepare('DESCRIBE ' . $tableName); - $stmt->execute(); - $columns = $stmt->fetchAll(PDO::FETCH_COLUMN); + // Query the database to get the grants for the current user + $sqlCheckPrivileges = "SHOW GRANTS FOR CURRENT_USER()"; + $stmtCheck = $pdo->prepare($sqlCheckPrivileges); + $stmtCheck->execute(); + $grants = $stmtCheck->fetchAll(PDO::FETCH_COLUMN); + + // Check if any of the grants include the INSERT privilege for the specified table or for all tables + foreach ($grants as $grant) { + if (strpos($grant, "GRANT INSERT ON *.*") !== false) { + return true; // Global insert privilege + } + if (strpos($grant, "GRANT INSERT ON `{$tableName}`") !== false || strpos($grant, "GRANT INSERT ON `$tableName`.*") !== false) { + return true; // Specific table or database-wide insert privilege + } + } + + return false; + } - // Generate the button to open the modal - $button = "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#insertModal'> - Add Data - </button>"; + function insertDataIntoTable($pdo, $tableName) { + $columns = array_keys($_POST); + unset($columns[array_search('tableName', $columns)]); // Exclude tableName from columns - // Generate the modal - $modal = generateStylizedModal($columns); + $placeholders = rtrim(str_repeat('?,', count($columns)), ','); + $sql = "INSERT INTO $tableName (" . implode(',', $columns) . ") VALUES ($placeholders)"; - echo $button . $modal; + $stmt = $pdo->prepare($sql); + $values = array_values($_POST); + array_pop($values); // Exclude tableName from values + $stmt->execute($values); } + + function generateInsertFunction($tableName) { + $db = Database::getInstance(); + $pdo = $db->getPdo(); + + if (isFormSubmitted() && isTableNameSet()) { + insertDataIntoTable($pdo, $tableName); + redirectToCurrentPage(); + } - function generateStylizedModal($columns) { - // Generate the modal - $modal = "<div class='modal fade' id='insertModal' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'> - <div class='modal-dialog' role='document'> - <div class='modal-content rounded'> - <div class='modal-header'> - <button type='button' class='close ml-0' data-dismiss='modal' aria-label='Close'> - <span aria-hidden='true'>&times;</span> - </button> - </div> - <div class='modal-body'>"; + $columns = fetchTableColumns($pdo, $tableName); + echo generateAddDataButton() . generateStylizedModal($tableName, $columns); + } + + function isFormSubmitted() { + return $_SERVER['REQUEST_METHOD'] === 'POST'; + } + + function isTableNameSet() { + return isset($_POST['tableName']); + } + + function redirectToCurrentPage() { + header('Location: ' . $_SERVER['PHP_SELF']); + exit; + } + + function fetchTableColumns($pdo, $tableName) { + $stmt = $pdo->prepare('DESCRIBE ' . $tableName); + $stmt->execute(); + return $stmt->fetchAll(PDO::FETCH_COLUMN); + } + function generateAddDataButton() { + return "<button type='button' class='btn btn-primary' data-toggle='modal' data-target='#insertModal'> + Add Data + </button>"; + } + + function generateStylizedModal($tableName, $columns) { + $modalStart = "<form action='' method='POST'> + <div class='modal fade' id='insertModal' tabindex='-1' role='dialog' aria-labelledby='insertModalLabel' aria-hidden='true'> + <div class='modal-dialog' role='document'> + <div class='modal-content rounded'> + <div class='modal-header'> + <button type='button' class='close ml-0' data-dismiss='modal' aria-label='Close'> + <span aria-hidden='true'>&times;</span> + </button> + </div> + <div class='modal-body'>"; + + $modalBody = ''; foreach ($columns as $column) { - $modal .= "<div class='form-group'> - <label for='$column'>$column</label> - <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' required> - </div>"; + $modalBody .= "<div class='form-group'> + <label for='$column'>$column</label> + <input type='text' class='form-control' id='$column' name='$column' placeholder='$column' required> + </div>"; } - $modal .= "</div> - <div class='modal-footer'> - <button type='button' class='btn btn-primary' onclick='saveData()'>Save</button> + $modalEnd = "</div> + <div class='modal-footer'> + <input type='hidden' name='tableName' value='$tableName'> + <button type='submit' class='btn btn-primary'>Save</button> + </div> </div> </div> - </div> - </div>"; + </form>"; - return $modal; + return $modalStart . $modalBody . $modalEnd; } ?> \ No newline at end of file diff --git a/Database.php b/Database.php @@ -0,0 +1,36 @@ +<?php + +// Singleton class for PDO connection + +class Database { + private static $instance = null; + private $pdo; + + private function __construct($host, $dbname, $username, $password) { + try { + $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password); + } catch (PDOException $e) { + die('Connection failed: ' . $e->getMessage()); + } + } + + public static function getInstance($host = null, $dbname = null, $username = null, $password = null) { + if (self::$instance === null) { + if ($host === null || $dbname === null || $username === null || $password === null) { + throw new Exception("Database connection parameters are required for the first call to getInstance."); + } + self::$instance = new Database($host, $dbname, $username, $password); + } + return self::$instance; + } + + public function getPdo() { + return $this->pdo; + } +} + +// Usage example: +// $db = Database::getInstance('localhost', 'mydb', 'root', 'password'); +// $pdo = $db->getPdo(); + +?> +\ No newline at end of file diff --git a/Incidents.php b/Incidents.php @@ -1,23 +0,0 @@ -<?php include 'Components.php'; generateHead(); ?> - -<body> - <?php - # --- HEAD --- # - generateNavbar(); - - # --- TABLES --- # - echo "<div class=\"m-5\">"; - - echo "<h2>Incidents</h2>"; - displayTable("Incident"); - - generateInsertFunction("Incident"); - - echo "</div>"; - - # --- FOOTER --- # - generateFooter(); - ?> -</body> - -</html> -\ No newline at end of file diff --git a/Terrain.php b/Terrain.php @@ -1,24 +0,0 @@ -<?php include 'Components.php'; generateHead(); ?> - -<body> - <?php - - - # --- HEAD --- # - - generateNavbar(); - - # --- TABLES --- # - echo "<div class=\"m-5\">"; - - echo "<h2>Terrain</h2>"; - displayTable("Terrain"); - - echo "</div>"; - - # --- FOOTER --- # - generateFooter(); - ?> -</body> - -</html> -\ No newline at end of file diff --git a/incidents.php b/incidents.php @@ -0,0 +1,29 @@ +<?php + require 'Database.php'; + include 'Components.php'; + + Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); + generateHead(); +?> + +<body> + <?php + # --- HEAD --- # + generateNavbar(); + + # --- TABLES --- # + echo "<div class=\"m-5\">"; + + echo "<h2>Incidents</h2>"; + displayTable("Incident"); + + generateInsertFunction("Incident"); + + echo "</div>"; + + # --- FOOTER --- # + generateFooter(); + ?> +</body> + +</html> +\ No newline at end of file diff --git a/index.php b/index.php @@ -1,11 +1,14 @@ -<?php include 'Components.php'; generateHead(); ?> +<?php + require 'Database.php'; + include 'Components.php'; + + Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); + generateHead(); +?> <body> <?php - - # --- HEAD --- # - generateNavbar(); # --- TABLES --- # diff --git a/operations.php b/operations.php @@ -1,11 +1,14 @@ -<?php include 'Components.php'; generateHead(); ?> +<?php + require 'Database.php'; + include 'Components.php'; + + Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); + generateHead(); +?> <body> <?php - - # --- HEAD --- # - generateNavbar(); # --- TABLES --- # diff --git a/terrain.php b/terrain.php @@ -0,0 +1,27 @@ +<?php + require 'Database.php'; + include 'Components.php'; + + Database::getInstance('mysql', 'a22willi', 'root', 'Safiren1'); + generateHead(); +?> + +<body> + <?php + # --- HEAD --- # + generateNavbar(); + + # --- TABLES --- # + echo "<div class=\"m-5\">"; + + echo "<h2>Terrain</h2>"; + displayTable("Terrain"); + + echo "</div>"; + + # --- FOOTER --- # + generateFooter(); + ?> +</body> + +</html> +\ No newline at end of file