From: Mischa POSLAWSKY Date: Thu, 7 Nov 2019 03:43:53 +0000 (+0100) Subject: issue: database insert method to keep array values X-Git-Tag: v4.2~29 X-Git-Url: http://git.shiar.net/minimedit.git/commitdiff_plain/f988982ad609256e3bbbb8a41ca7c7b0b3b4fb74 issue: database insert method to keep array values Feature to apply literal SQL with optional arguments, stolen from other projects modeled after DBIx::Simple. --- 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); } }