{"record":{"id":"772038f00d286292","repo":"phalcon/cphalcon","slug":"the-number-of-values-in-the-update-is-not-the-same","errorCode":null,"errorMessage":"The number of values in the update is not the same as fields","messagePattern":"The number of values in the update is not the same as fields","errorType":"exception","errorClass":"Phalcon\\Db\\Exceptions\\UpdateFieldCountMismatch","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Adapter/AbstractAdapter.zep","lineNumber":1412,"sourceCode":"     * Warning! If $whereCondition is string it not escaped.\n     */\n    public function update(string table, var fields, var values, var whereCondition = null, var dataTypes = null) -> bool\n    {\n        var bindDataTypes, conditions, escapedField, escapedTable,\n            field, placeholder, placeholders, position, setClause, tableName,\n            updateSql, updateValues, value, whereBind, whereTypes;\n\n        let placeholders  = [],\n            updateValues  = [],\n            bindDataTypes = [];\n\n        /**\n         * Objects are casted using __toString, null values are converted to\n         * string 'null', everything else is passed as '?'\n         */\n        for position, value in values {\n            if unlikely !fetch field, fields[position] {\n                throw new UpdateFieldCountMismatch();\n            }\n\n            let escapedField = this->escapeIdentifier(field);\n            let placeholder  = this->buildValuePlaceholder(value, position, dataTypes);\n\n            let placeholders[] = escapedField . \" = \" . placeholder[\"placeholder\"];\n\n            if placeholder[\"bind\"] {\n                let updateValues[] = placeholder[\"value\"];\n\n                if placeholder[\"hasBindType\"] {\n                    let bindDataTypes[] = placeholder[\"bindType\"];\n                }\n            }\n        }\n\n        /**\n         * Check if we got table and schema and escape it accordingly","sourceCodeStart":1394,"sourceCodeEnd":1430,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Adapter/AbstractAdapter.zep#L1394-L1430","documentation":"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.","triggerScenarios":"$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.","commonSituations":"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.","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()"],"exampleFix":"// before\n$db->update('users', ['name'], ['John', time()]); // 1 field, 2 values\n\n// after\n$pairs = ['name' => 'John', 'updated_at' => time()];\n$db->update('users', array_keys($pairs), array_values($pairs));","handlingStrategy":"validation","validationCode":"if (count($values) !== count($fields)) {\n    throw new InvalidArgumentException('update(): fields and values must have equal counts');\n}\n$connection->update($table, $fields, $values, $whereCondition, $dataTypes);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["php","phalcon","db","update","argument-mismatch","validation"],"backgroundTag":"column-value-count-mismatch","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}