update()

public function update(int $id, array $data, string $table): bool

Description

Updates a specific record in a database table identified by its ID. This method validates table existence, constructs a parameterized UPDATE query from an associative array of column-value pairs, and uses prepared statements to prevent SQL injection. Returns true if the update operation was successful.

Parameters

Parameter Type Description Default Required
id int The ID of the record to update. - Yes
data array Associative array where keys are column names and values are the new data to update. - Yes
table string The name of the database table containing the record to update. - Yes

Return Value

Type Description
bool Returns true if the update query executed successfully, false otherwise.

Exceptions

  • RuntimeException - If the specified table does not exist in the database.
  • PDOException - If there's a database error during query execution.

Example #1: Updating a User Record

The code sample below demonstrates how to update a user record with ID 15 in the 'users' table.

PHP
$user_id = 15;
$update_data = [
    'name' => 'John Smith',
    'email' => '[email protected]',
    'updated_at' => date('Y-m-d H:i:s')
];

$success = $this->db->update($user_id, $update_data, 'users');

if ($success) {
    echo "User record updated successfully!";
} else {
    echo "Failed to update user record.";
}

Example #2: Updating a Product Record

The code sample below demonstrates how to update a product's price and stock quantity.

PHP
$product_id = 42;
$product_data = [
    'price' => 89.99,
    'stock_quantity' => 150,
    'last_updated' => date('Y-m-d H:i:s')
];

$success = $this->db->update($product_id, $product_data, 'products');

if ($success) {
    echo "Product #" . $product_id . " has been updated.";
}

Example #3: Updating with Different Data Types

The code sample below demonstrates updating a record with various data types including null values.

PHP
$order_id = 1001;
$order_updates = [
    'status' => 'shipped',          // string
    'shipping_date' => date('Y-m-d'), // date string
    'notes' => null,                // null value
    'priority' => 1,                // integer
    'discount_applied' => 15.50     // float
];

$result = $this->db->update($order_id, $order_updates, 'orders');
echo $result ? "Order updated." : "Update failed.";

Important Notes

  • All three parameters ($id, $data, and $table) are required.
  • The method validates that the table exists before executing the UPDATE query.
  • Column names from the $data array are automatically escaped with backticks in the SQL query.
  • The ID value is automatically added to the data array with key 'id' for parameter binding.
  • Uses prepared statements with named parameters for security against SQL injection.
  • Returns true if the query executes successfully, even if no rows were actually changed (e.g., if data is identical).
  • Data types are automatically handled (strings, integers, floats, booleans, null values).
  • If debug mode is enabled, the generated SQL query will be displayed before execution.
  • Only updates records where the primary key column is named 'id'.