CodeIgniter控制器未定义属性问题:表单数据插入模型报错
Fixing "Undefined Property" Error in CodeIgniter Controller When Using Models
Hey there, it looks like you're hitting an "undefined property" error when trying to use your prueba_model in the submit method of your CodeIgniter controller. Let's break down the common causes and fixes step by step:
1. Double-Check Model Naming & File Structure
CodeIgniter has strict naming rules for models—this is the #1 cause of this error:
- Your model file must be named with a capitalized first letter, e.g.,
Prueba_model.php(note the uppercase "P") - The file needs to live directly in
application/models/(or in a subfolder, which we'll cover later) - The model class name must match the filename exactly, like this:
<?php class Prueba_model extends CI_Model { public function insert_data() { // Example insert logic (adjust to your table/fields) $form_data = [ 'name' => $this->input->post('name'), 'email' => $this->input->post('email') ]; return $this->db->insert('your_table', $form_data); } }
2. Verify Model Loading & Syntax in Your Controller
In your submit method, make sure you're loading and calling the model correctly:
public function submit(){ // Load the model—use lowercase here even if the filename is capitalized $this->load->model('prueba_model'); // Call the model method (match the model name you used when loading) $this->prueba_model->insert_data(); // Also, note your code cuts off at $this->input->pos...—make sure you use the full syntax: $this->input->post('field_name') }
If your model is in a subfolder like application/models/forms/, load it with the full path:
$this->load->model('forms/prueba_model');
3. Troubleshoot Common Mistakes
- Typos: Double-check spelling for the model name, method name, and even table names—one wrong character breaks everything!
- Missing Inheritance: Your model class must extend
CI_Model—if it doesn't, CodeIgniter won't recognize it as a valid model. - File Permissions: Ensure your model file has read permissions for your server—sometimes permission issues prevent the file from being loaded.
- CI Version: Your controller code looks like CodeIgniter 3.x, but if you're using 4.x, the model system uses namespaces and different loading syntax—confirm your version matches your code style.
4. Debug to Confirm Loading
If you're still stuck, add a debug line to check if the model is actually loading:
public function submit(){ $this->load->model('prueba_model'); // Print all loaded models to verify prueba_model is present var_dump($this->load->get_models()); $this->prueba_model->insert_data(); }
内容的提问来源于stack exchange,提问作者afs_
相关产品推荐
相关产品推荐

