CodeIgniter数据插入失败无报错求助:新手遇数据库未写入问题
Hey there, fellow CodeIgniter newbie! I’ve been in your shoes before—frustrated when data doesn’t save to the database and there’s no error message to clue you in. Let’s walk through the most common fixes for your issue:
1. Verify Your Form Setup First
Double-check that your HTML form is correctly configured:
- Make sure your form uses the
POSTmethod:<form method="post" action="<?php echo base_url('register/daftar_akun'); ?>"> - Confirm all input
nameattributes match the keys you’re using in$this->input->post()(e.g.,name="reg_username"for the username field). If these don’t match, you’ll be inserting empty values, which might fail silently if your database has non-null constraints.
2. Enable Debugging to Uncover Hidden Errors
CodeIgniter might not show errors by default, so let’s turn on debugging to see what’s really happening:
- Open
application/config/config.phpand set:$config['log_threshold'] = 2; // Logs errors and messages $config['debug'] = TRUE; // Shows debug info in the browser - Modify your Model to log database errors:
class M_register extends CI_Model{ function register_akun($data){ $this->db->insert('user',$data); // Check for database errors $error = $this->db->error(); if (!empty($error['code'])) { log_message('error', "Insert Failed: Code {$error['code']} - {$error['message']}"); return false; } return $this->db->insert_id(); // Return the new user ID on success } } - Update your Controller to handle the result and log issues:
public function daftar_akun(){ // ... (your existing code to get post data) $result = $this->m_register->register_akun($data); if (!$result) { log_message('error', "Failed to register user with data: " . print_r($data, true)); // Redirect back to register page with an error message (optional) $this->session->set_flashdata('error', 'Registration failed. Please try again.'); redirect(base_url("register")); } else { redirect(base_url("login")); } }
Check the application/logs folder after trying to register—you’ll likely find the root cause here.
3. Validate Database & Field Matching
- Confirm your
usertable exists in the database specified inapplication/config/database.php. - Make sure all keys in your
$dataarray exactly match the column names in theusertable (case-sensitive, depending on your database setup). For example, if your column isnama_lengkap, don’t usenamaLengkapin the array. - Check for column constraints: Are any columns set to
NOT NULLthat you’re not providing a value for? Or do you have a unique constraint (like onusernameoremail) that’s being violated?
4. Quick Security & Best Practice Tip
While not the cause of your insert failure, you should always hash passwords before storing them. Update your password line in the Controller:
$password = password_hash($this->input->post('reg_password'), PASSWORD_DEFAULT);
Try these steps one by one—chances are one of them will fix your silent insert failure. Let me know if you find the issue!
内容的提问来源于stack exchange,提问作者Bani Aja Lah

