From: Mischa POSLAWSKY Date: Thu, 17 Oct 2019 03:53:55 +0000 (+0200) Subject: nieuws/replies: insert method to add database rows X-Git-Tag: v4.1~11 X-Git-Url: http://git.shiar.nl/minimedit.git/commitdiff_plain/ce6d14a5ee9d14d5573798e635e1ffe47cf65602 nieuws/replies: insert method to add database rows Minimal database abstraction for code maintainability (feature similar to perl DBIx::Simple). --- diff --git a/database.inc.php b/database.inc.php index aa864d0..6a82507 100644 --- a/database.inc.php +++ b/database.inc.php @@ -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)); + } } diff --git a/widget/nieuws/replies.php b/widget/nieuws/replies.php index 13533c9..cfa9487 100644 --- a/widget/nieuws/replies.php +++ b/widget/nieuws/replies.php @@ -8,8 +8,10 @@ if ($_POST) { try { $html = nl2br(htmlspecialchars($_POST['reply'])); $html = "

$html

"; - $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');