{"record":{"id":"e0926998a927863f","repo":"phalcon/cphalcon","slug":"unable-to-insert-into-table-without-data","errorCode":null,"errorMessage":"Unable to insert into {table} without data","messagePattern":"Unable to insert into (.+?) without data","errorType":"exception","errorClass":"Phalcon\\Db\\Exceptions\\CannotInsertWithoutData","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Adapter/AbstractAdapter.zep","lineNumber":935,"sourceCode":"     *     [\"Test Invoice\", 100],\n     *     [\"inv_title\", \"inv_total\"]\n     * );\n     *\n     * // Next SQL sentence is sent to the database system\n     * INSERT INTO `co_invoices` (`inv_title`, `inv_total`) VALUES (\"Test Invoice\", 100);\n     * ```\n     */\n    public function insert(string table,  array values, var fields = null, var dataTypes = null) -> bool\n    {\n        var bindDataTypes, escapedTable, escapedFields, field,\n            insertSql, insertValues, joinedValues, placeholder, placeholders,\n            position, tableName, value;\n\n        /**\n         * A valid array with more than one element is required\n         */\n        if unlikely empty values {\n            throw new CannotInsertWithoutData(table);\n        }\n\n        let placeholders  = [],\n            insertValues  = [],\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            let placeholder = this->buildValuePlaceholder(value, position, dataTypes);\n\n            let placeholders[] = placeholder[\"placeholder\"];\n\n            if placeholder[\"bind\"] {\n                let insertValues[] = placeholder[\"value\"];\n","sourceCodeStart":917,"sourceCodeEnd":953,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Adapter/AbstractAdapter.zep#L917-L953","documentation":"insert() requires a non-empty $values array ('if unlikely empty values' throws CannotInsertWithoutData). An INSERT with zero values cannot be generated (there would be no columns and no placeholders), so the adapter refuses immediately, before any SQL is built.","triggerScenarios":"$db->insert('users', []) — typically because the payload was built from a filtered request, a loop over an empty collection, or array_filter() removing every element; also array_map over an empty source list.","commonSituations":"Mass-assignment code where all keys are stripped by a whitelist filter; batch insert loops where one chunk is empty; form handlers that insert on POST bodies with no relevant fields; CSV/import pipelines hitting an empty record.","solutions":["Guard before calling: if (!empty($values)) { $db->insert(...); }","If the empty payload is a bug upstream, fix the source that produced it rather than skipping","For optional inserts, decide explicitly: skip silently or raise your own validation error to the caller"],"exampleFix":"// before\n$db->insert('users', $filtered); // $filtered = [] after whitelist filtering\n\n// after\nif (!empty($filtered)) {\n    $db->insert('users', $filtered);\n}","handlingStrategy":"validation","validationCode":"if (empty($values)) {\n    // nothing to insert — skip or surface a domain error\n    return;\n}\n$connection->insert($table, $values, $fields, $dataTypes);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard every insert() whose payload comes from user input or filters with !empty($values)","In batch importers, skip empty chunks explicitly instead of passing them through","Validate required fields at the boundary (request validation) so empty payloads never reach the DB layer"],"tags":["php","phalcon","db","insert","empty-data","validation"],"backgroundTag":"empty-insert-values","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}