Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ public function insert($table, $data)
*
* @param string $table table name
* @param array $data array of columns and values
* @param array $where array of columns and values
* @param object $where array of columns and values or string of query
* @param array $args params of query
*/
public function update($table, $data, $where)
public function update($table, $data, $where, $args = [])
{
//collect the values from data and where
$values = [];

//setup fields
$fieldDetails = null;
foreach ($data as $key => $value) {
Expand All @@ -212,16 +213,30 @@ public function update($table, $data, $where)

//setup where
$whereDetails = null;
$i = 0;
foreach ($where as $key => $value) {
$key = '`' . trim($key, '`') . '`';
$whereDetails .= $i == 0 ? "$key = ?" : " AND $key = ?";
$values[] = $value;
$i++;
if (is_string($where)) {
$where = trim($where);
$whereDetails = $where ?: '';
$values = array_merge($values, $args);
}

$stmt = $this->run("UPDATE $table SET $fieldDetails WHERE $whereDetails", $values);

else {
$i = 0;
foreach ($where as $key => $value) {
$key = '`' . trim($key, '`') . '`';
$whereDetails .= $i == 0 ? "$key = ?" : " AND $key = ?";
$values[] = $value;
$i++;
}
}

// build SQL statement
$sql = "UPDATE $table SET $fieldDetails";
if ($whereDetails) {
$sql .= " WHERE $whereDetails";
}

//execute query
$stmt = $this->run($sql, $values);

return $stmt->rowCount();
}

Expand Down