nieuws/replies: insert method to add database rows
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 17 Oct 2019 03:53:55 +0000 (05:53 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sun, 27 Oct 2019 00:36:38 +0000 (02:36 +0200)
Minimal database abstraction for code maintainability
(feature similar to perl DBIx::Simple).

database.inc.php
widget/nieuws/replies.php

index aa864d03df32f5dadedba9441512331f5fe10d5c..6a82507649612a5739bf3fb396062fb1cae2e686 100644 (file)
@@ -18,4 +18,14 @@ class DB
                $stmt->execute($params);
                return $stmt;
        }
+
+       function insert($table, $row)
+       {
+               $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)',
+                       '"'.$table.'"',
+                       implode(', ', array_keys($row)),
+                       implode(', ', array_fill(0, count($row), '?'))
+               );
+               return $this->query($sql, array_values($row));
+       }
 }
index 13533c99fe09a35049f23d918ced22ecd5e591cb..cfa948715367086f7fd0ce663b7083cf416a75c8 100644 (file)
@@ -8,8 +8,10 @@ if ($_POST) {
        try {
                $html = nl2br(htmlspecialchars($_POST['reply']));
                $html = "<p>$html</p>";
-               $query = $Db->query('INSERT INTO comments (page, message, author) VALUES (?, ?, ?)', [
-                       $Page, $html, $User->login
+               $query = $Db->insert('comments', [
+                       'page'    => $Page,
+                       'message' => $html,
+                       'author'  => $User->login,
                ]);
                if (!$query->rowCount()) {
                        throw new Exception('Fout bij opslaan');