phalcon/cphalcon · error · Phalcon\Db\Exceptions\UpdateFieldCountMismatch
The number of values in the update is not the same as fields
Error message
The number of values in the update is not the same as fields
What it means
update(table, fields, values) iterates $values by position and looks up fields[position] to build the SET clause. When $values has more entries than $fields, the lookup fails and UpdateFieldCountMismatch ('The number of values in the update is not the same as fields') is thrown. The fields and values arrays must be parallel lists: same length, same order.
Source
Thrown at phalcon/Db/Adapter/AbstractAdapter.zep:1412
* Warning! If $whereCondition is string it not escaped.
*/
public function update(string table, var fields, var values, var whereCondition = null, var dataTypes = null) -> bool
{
var bindDataTypes, conditions, escapedField, escapedTable,
field, placeholder, placeholders, position, setClause, tableName,
updateSql, updateValues, value, whereBind, whereTypes;
let placeholders = [],
updateValues = [],
bindDataTypes = [];
/**
* Objects are casted using __toString, null values are converted to
* string 'null', everything else is passed as '?'
*/
for position, value in values {
if unlikely !fetch field, fields[position] {
throw new UpdateFieldCountMismatch();
}
let escapedField = this->escapeIdentifier(field);
let placeholder = this->buildValuePlaceholder(value, position, dataTypes);
let placeholders[] = escapedField . " = " . placeholder["placeholder"];
if placeholder["bind"] {
let updateValues[] = placeholder["value"];
if placeholder["hasBindType"] {
let bindDataTypes[] = placeholder["bindType"];
}
}
}
/**
* Check if we got table and schema and escape it accordinglyView on GitHub (pinned to b7419de9cd)
Solutions
- Keep the arrays parallel: count($fields) === count($values), same order
- Build them as one structure and split at call time to guarantee alignment: $pairs = ['name' => $x, 'updated_at' => $t]; $db->update('users', array_keys($pairs), array_values($pairs))
- If arrays are built separately, assert count equality before calling update()
Example fix
// before
$db->update('users', ['name'], ['John', time()]); // 1 field, 2 values
// after
$pairs = ['name' => 'John', 'updated_at' => time()];
$db->update('users', array_keys($pairs), array_values($pairs)); Defensive patterns
Strategy: validation
Validate before calling
if (count($values) !== count($fields)) {
throw new InvalidArgumentException('update(): fields and values must have equal counts');
}
$connection->update($table, $fields, $values, $whereCondition, $dataTypes); Prevention
- Derive fields and values from one source: $db->update($t, array_keys($pairs), array_values($pairs))
- Never apply array_filter/array_slice to only one of the two arrays
- Watch the argument order: fields (names) come before values
When it happens
Trigger: $db->update('users', ['name'], ['John', time()]) — 2 values for 1 field; swapping the second and third arguments; appending a value to $values without appending the matching field name; building the two arrays in separate loops that diverge.
Common situations: Dynamic update builders where fields are whitelisted but values are not (or vice versa); argument-order confusion since fields comes before values; cond-itional field removal via array_filter applied to only one of the two arrays.
Related errors
- Unable to insert into {table} without data
- Invalid WHERE clause conditions
- Incomplete number of bind types
- The 'dialectClass' '{className}' must implement Phalcon\Db\D
- Savepoints are not supported by this database adapter
AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21).
Data as JSON: /api/errors/772038f00d286292.
Report an issue: GitHub.