Honeypot causing error |
Hello,
I enabled honeypot in config and now get this error when saving a record Code: if (!$record->hasChanged()) { Unknown column 'honeypot' in 'field list' I had to add unset to resolve this. Code: $post = $this->request->getPost();
This field will be automatically filtered out if you do not disable field protection.
(03-03-2025, 11:37 PM)michalsn Wrote: This field will be automatically filtered out if you do not disable field protection. Thank you, I tried that and now I get an error. There is no data to update. Code: $post = $this->request->getPost();
In your model set $updateOnlyChanged to false.
https://codeigniter.com/user_guide/model...nlychanged (03-04-2025, 07:39 AM)michalsn Wrote: In your model set $updateOnlyChanged to false. Thank you
This happens because the honeypot field is included in the POST data, but your database table doesn’t actually have a column named honeypot. When you call $model->save() or $model->fill(), CodeIgniter tries to map all fields from the POST array to your database columns—leading to the Unknown column 'honeypot' in 'field list' error.
Why unset($post['honeypot']) fixes it By unsetting the honeypot index from the POST array, you’re removing that field from the data you pass into $model->fill($data). As a result, CodeIgniter no longer tries to save the honeypot field to the database. Alternative Approaches Use $allowedFields in your Model In CodeIgniter 4, if you set $protectFields = true; and specify only the valid column names in $allowedFields, any extra fields (like honeypot) will be ignored automatically. php Копировать class PageModel extends Model { protected $table = 'pages'; protected $primaryKey = 'id'; protected $allowedFields = ['page_title', 'page_url', /* etc */]; protected $protectFields = true; // ... } Then, calling $model->save($data) or $model->fill($data)->save() will skip any fields not listed in $allowedFields, so honeypot will not be inserted. Manually remove/ignore honeypot As you’ve already done, you can unset($post['honeypot']); before filling. This is perfectly fine if you only have a couple of forms that send a honeypot field. Another option is to conditionally remove honeypot from the data in a global filter, or in your controller logic, before you ever call $model->save(). Custom Validation/Logic If you want to detect the honeypot field being filled (e.g., to block bots), you could add extra logic in your controller or model. But typically, the built-in honeypot setting just needs you to avoid saving that field to the database. Summary The error arises because CodeIgniter’s model tries to persist a field (honeypot) that your table doesn’t have. You can remove that field from the data (manually via unset()) or ignore it automatically by setting $allowedFields and $protectFields = true; in the corresponding model. Either way, as long as the honeypot field is never mapped onto a real table column, the error will be resolved. |
Welcome Guest, Not a member yet? Register Sign In |