DB::update (method)

Last updated: Tue, Oct 31, 2023 1:43 pm
Return to Knowledgebase

Purpose

Update records in a specified database table.

Location

users/classes/DB.php

Parameters

# Parameter Data Type Required Description
1 $table string Yes Name of database table to update
2 $id int or arry Yes Unique identifiers of records to be updated. If array, it specifies a set of conditions for the update
3 $fields array Yes Associative array with field names as keys and values to be updated

Returns

Data Type Description of Returned Data
bool "true" if operations is successful; "false" if unsuccessful

Further Documentation:

1. The update method in the UserSpice class is used to update records in a specified database table.
2. It generates and executes an SQL UPDATE statement based on the provided table name, unique identifier (id), and field-value pairs to be updated.


// Updating a record in the 'users' table with ID 5

$fields = [
'username' => 'john_doe_updated',
'email' => 'john_doe_updated@example.com'
];
$result = $db->update('users', 5, $fields);

// Updating records based on specific conditions
$conditions = [
'role' => 'admin',
'active' => 1
];
$result = $db->update('users', $conditions, $fields);