CodeIgniter框架PHP代码本地可插数据库,虚拟主机环境插入失败求助
Hey there, let's walk through why your resume data isn't inserting into the database on your virtual host, even though it works perfectly locally. Here are the most common fixes to check step by step:
1. Verify Database Configuration & Permissions
First, double-check your database settings in application/config/database.php—virtual hosts almost always use different credentials than your local environment:
- Confirm the
hostname,username,password, anddatabasevalues match what your hosting provider gave you. - Ensure your database user has INSERT permissions for the
resumetable. Local environments often use root accounts with full access, but shared hosting users usually have restricted permissions. You can check this via your host's phpMyAdmin or database control panel. - Watch out for table name case sensitivity! Linux-based virtual hosts are case-sensitive, while Windows (local) isn't. If your local table is named
Resumebut the host's table isresume, the insert will fail silently.
2. Debug POST Data Receipt
Your resume_form method has echo statements, but the redirect will hide them. Let's log the POST data to confirm it's reaching the controller:
Add this line at the start of resume_form():
log_message('debug', 'Received POST data: ' . print_r($this->input->post(), true));
Then check application/logs/ for the debug log. If the POST data is empty, you might have:
- A broken form action (ensure you're using
form_open('recruit/resume_form')which handles CI's URL structure correctly). - CSRF protection enabled on the host but missing in your form. If
$config['csrf_protection'] = TRUEinconfig.php, add<?php echo form_hidden($this->security->get_csrf_token_name(), $this->security->get_csrf_hash()); ?>to your form, or useform_open()which auto-injects it.
3. Log Database Insert Errors
Your model's saveResume method doesn't check for database errors. Let's add error logging to catch what's going wrong:
Update your model method like this:
function saveResume($data) { $this->db->insert('resume', $data); // Log any database errors if ($db_error = $this->db->error()) { log_message('error', 'Insert failed: ' . $db_error['message'] . ' (Code: ' . $db_error['code'] . ')'); } return $this->db->insert_id(); }
Check the error logs for messages like "Unknown column 'type' in 'field list'" or "Duplicate entry"—these will point directly to the issue (e.g., missing columns, wrong data types).
4. Check URL & Rewrite Configuration
- Confirm
$config['base_url']inapplication/config/config.phpis set to your virtual host's domain (e.g.,https://yourdomain.com/). A wrong base_url can break redirects and form actions. - Ensure your
.htaccessfile is correctly configured to removeindex.phpfrom URLs. A common working.htaccessfor CI is:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If your host doesn't allow URL rewrites, you'll need to include index.php in your form action and redirect URL.
5. Fix Minor Code Issue
Quick note: Your saveResume method has an extra set of curly braces that don't affect functionality but can be cleaned up:
// Remove the extra braces here function saveResume($data) { $this->db->insert('resume', $data); $resume_id = $this->db->insert_id(); return $resume_id; }
Start with enabling error logging and checking database credentials—those are the most frequent culprits for this kind of issue. Once you have the error log details, it'll be much easier to pinpoint the exact problem!
内容的提问来源于stack exchange,提问作者Chanatip Rattanapahu

