该表单的验证工作正常,但是当我尝试插入和提交时,数据没有存储在数据库中。我不明白我遇到了哪个问题。
Home.php(控制器文件)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct()
{
parent::__construct();
//load database libray manually
$this->load->database();
//load Model
$this->load->model('Contact_model');
// load form and url helpers
$this->load->helper(array('form', 'url'));
// load form_validation library
$this->load->library('form_validation');
}
public function contact()
{
// Basic validation
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('subject', 'Subject', 'required');
$this->form_validation->set_rules('message', 'Message', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->helper('url');
$this->load->view('header');
$this->load->view('contact');
$this->load->view('footer');
}
else
{
/* load success template...
echo "Thanks For Contacting!";*/
//insert
if($this->input->post('submit'))
{
$name=$this->input->post('name');
$email=$this->input->post('email');
$subject=$this->input->post('subject');
$message=$this->input->post('message');
$this->Contact_model->saverecords($name,$email,$subject,$message);
echo "Records Saved Successfully";
}
}
}
}
?>
contact_model.php(模型文件)
<?php
class Contact_model extends CI_Model
{
//Insert
function saverecords($name,$email,$subject,$message)
{
$query="insert into contact values('', '$name', '$email', '$subject', '$message')";
$this->db->query($query);
}
}
?>