Skip to content

Commit

Permalink
see #469 - try and make sure we do update timestamp fields
Browse files Browse the repository at this point in the history
They may not be present in our $values array, but update them anyway.
  • Loading branch information
DavidGoodwin committed Apr 6, 2021
1 parent d5c1424 commit 04f20d9
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1945,25 +1945,32 @@ function db_insert($table, array $values, $timestamp = array('created', 'modifie
* @param array $timestamp (optional) - array of fields to set to now() - default: array('modified')
* @return int - number of updated rows
*/
function db_update($table, $where_col, $where_value, $values, $timestamp = array('modified'), $throw_exceptions = false) {
function db_update(string $table, string $where_col, string $where_value, array $values, array $timestamp = array('modified'), bool $throw_exceptions = false):int {
$table_key = table_by_key($table);

$sql = "UPDATE $table_key SET ";

$pvalues = array();

$set = array();

foreach($timestamp as $k) {
if(!isset($values[$k])) {
$values[$k] = 'x'; // timestamp field not in the values list, add it in so we set it to now() see #469
}
}

foreach ($values as $key => $value) {
if (in_array($key, $timestamp)) {
if (db_sqlite()) {
$set[] = " $key = datetime('now') ";
} else {
$set[] = " $key = now() ";
}
} else {
$set[] = " $key = :$key ";
$pvalues[$key] = $value;
continue;
}

$set[] = " $key = :$key ";
$pvalues[$key] = $value;

}

$pvalues['where'] = $where_value;
Expand Down

0 comments on commit 04f20d9

Please sign in to comment.