我正在使用codeigniter概念将记录保存在数据库中,这种编码方式我有一点问题,请有人可以帮助我,因为我是这个概念的新手, 表单的验证工作正常,但是当我尝试插入并提交时,数据未存储在数据库中,即时通讯没有收到即时通讯,请面对 谁能帮助我解决这个问题。
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);
}
}
?>
答案 0 :(得分:0)
您的查询似乎有误,您需要描述您要更新的列,我想格式应该是
$sql = "INSERT INTO foobar(firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";