CodeIgniter表单无法提交至数据库求助:PHP新手开发问题排查
Hey there! Let's walk through your code step by step—there are a few straightforward issues here that are likely causing your form not to submit to the database, rather than a connection problem (though we'll cover that too just in case).
Key Issues in Your Code:
- Mismatched Variable Name in Controller: In your
elseblock, you're passing$formArraytocreate1()but you defined the array as$formArray1. This means you're sending an undefined variable to your model, so nothing gets inserted. - Missing
nameAttribute in Textarea: Your<textarea>only has anidbut noname="post"attribute. CodeIgniter can't retrieve the value of the field without this, so$this->input->post('post')returns nothing. - Undefined
$userVariable inelseBlock: You define$userinside theifblock, but it's not accessible in theelseblock. Trying to use$user['ID']here will throw an error. - Mismatched Field Name in
form_error(): You're checking forform_error('posts')but your validation rule uses'post'—this means error messages won't show up correctly even if validation fails.
Fixed Code Snippets:
Corrected Controller Function:
public function status(){ $this->load->library('form_validation'); $this->form_validation->set_rules('post', 'Post Something', 'required'); // Move $user outside the if block so it's accessible everywhere $user = $this->session->userdata('user'); if ($this->form_validation->run() == FALSE) { $data1['user'] = $user; $this->load->view('pages/panel',$data1); } else{ $this->load->model('reg_model'); $formArray1 = array(); $formArray1['post'] = $this->input->post('post'); $formArray1['posted_by'] = $user['ID']; // Fix the variable name here to match $formArray1 $this->reg_model->create1($formArray1); // Update flash message to reflect it's a post, not an account $this->session->set_flashdata('msg', 'Your post has been created successfully'); redirect(base_url().'index.php/Registration/panel'); } }
Corrected Form Code:
<?php echo validation_errors();?> <form action = "<?php echo base_url()?>registration/status" name= "registrationForm1" id="registrationForm1" method = "post"> <label class ="form-control" for="post">Post Something</label></br> <!-- Add name="post" to the textarea so its value is submitted --> <textarea style ="width:100%" class="form-control" rows="3" id="post" name="post" placeholder="Your message..."></textarea> <!-- Fix form_error to use 'post' instead of 'posts' --> <p class ="invalid-feedback"><?php echo form_error('post');?></p></br> <button type="submit" class="btn btn-primary btn-block">Post</button> </form>
Improved Model Code (with Debugging):
public function create1($formArray1) { // Return the result of the insert to help debug if needed return $this->db->insert('posts', $formArray1); }
Checking Database Connection (If Issues Persist):
If you've fixed the above and still aren't seeing data in the database, verify your connection settings in application/config/database.php—make sure hostname, username, password, and database are all correct for your environment.
You can also add a quick check in your controller to confirm the connection:
// Add this somewhere in your status() function to debug connection if (!$this->db->conn_id) { die('Database connection failed: ' . print_r($this->db->error(), true)); }
Give these fixes a try—this should resolve the submission issue. If you run into any other errors, feel free to share the error messages!
内容的提问来源于stack exchange,提问作者Shashank Singh

