Skip to main content

Update Row Function

The update_row function allows you to modify existing records in the database based on specific conditions. This is useful when updating user data, modifying settings, or making bulk edits.


How to Use the update_row Function

Basic Usage

Update a specific field for rows that match a condition.

$updateConditions = ["username" => "alice"];
$newData = ["status" => "active"];

$sdb->update_row("users", $updateConditions, $newData);

This updates all records where username is "alice" and sets their status to "active".


Updating Multiple Fields

You can update multiple values at once.

$updateConditions = ["role" => "editor"];
$newData = [
"role" => "admin",
"permissions" => "full"
];

$sdb->update_row("users", $updateConditions, $newData);

This changes all "editor" users into "admin" and grants "full" permissions.


Using Conditions with Operators

You can specify conditions using comparison operators:

$updateConditions = [
"age" => [">", 30]
];
$newData = [
"membership" => "gold"
];

$sdb->update_row("users", $updateConditions, $newData);

This upgrades all users older than 30 to "gold" membership.


Using AND & OR Conditions

The function supports complex conditions:

Using AND (All conditions must be met)

$updateConditions = [
"AND" => [
["country" => "USA"],
["age" => [">=", 21]]
]
];
$newData = ["eligible" => true];

$sdb->update_row("users", $updateConditions, $newData);

This updates users from the USA who are 21 or older, setting "eligible" to true.


Using OR (At least one condition must be met)

$updateConditions = [
"OR" => [
["country" => "India"],
["country" => "Canada"]
]
];
$newData = ["discount" => 10];

$sdb->update_row("users", $updateConditions, $newData);

This applies a 10% discount to users from India or Canada.


When to Use update_row

Use update_row when:

  • You need to modify existing records.
  • You want to update multiple fields at once.
  • You want to apply updates based on complex conditions.

Do NOT use update_row when:

  • You need to insert new records (use add_row instead).
  • You want to retrieve data (use get_row instead).

Final Thoughts

The update_row function makes data updates easy and efficient in SwartzDB. Whether updating a single field or applying bulk changes, this function gives you full control over modifications.