From f988982ad609256e3bbbb8a41ca7c7b0b3b4fb74 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 7 Nov 2019 04:43:53 +0100 Subject: [PATCH] issue: database insert method to keep array values Feature to apply literal SQL with optional arguments, stolen from other projects modeled after DBIx::Simple. --- database.inc.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/database.inc.php b/database.inc.php index 2183358..464126b 100644 --- a/database.inc.php +++ b/database.inc.php @@ -22,13 +22,24 @@ class DB return $stmt; } + function _value($val, &$params) + { + $params[] = $val; + return '?'; + } + function insert($table, $row) { + $params = []; + $cols = []; + foreach ($row as $col => $val) { + $cols[] = $this->_value($val, $params); + } $sql = sprintf('INSERT INTO %s (%s) VALUES (%s)', '"'.$table.'"', implode(', ', array_keys($row)), - implode(', ', array_fill(0, count($row), '?')) + implode(', ', $cols) ); - return $this->query($sql, array_values($row)); + return $this->query($sql, $params); } } -- 2.30.0