我有一个简单的表单设置。它显示正常,当我点击提交看起来它正在工作,但当我去检查MSSQL Server中的数据它不存在。我不确定为什么它不起作用。
控制器是insert.php
<?php
class insert extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('insert_model');
}
function index()
{
// Including Validation Library
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
// Validating Name Field
$this->form_validation->set_rules('EmpName', 'Employee_Name', 'required|min_length[3]|max_length[15]');
// Validating Email Field
$this->form_validation->set_rules('Department', 'Department', 'required|min_length[3]|max_length[15]');
// Validating Email Field
$this->form_validation->set_rules('LanID', 'LanID', 'required|min_length[3]|max_length[15]');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('insert_view');
}
else
{
// Setting Values For Tabel Columns
$data = array(
'Employee_Name' => $this->input->post('EmpName'),
'Department' => $this->input->post('Department'),
'LanID' => $this->input->post('LanID'),
);
// Transfering Data To Model
$this->insert_model->form_insert($data);
// Loading View
$this->load->view('insert_view');
}
}
}
?>
模型是insert_model.php
<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(requests) of Database(employee)
$this->db->insert('requests', $data);
}
}
?>
查看是insert_view.php
<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
</head>
<body>
<div id="container">
<?php echo form_open('insert'); ?>
<h1>Insert Data Into Database Using CodeIgniter</h1>
<?php echo form_label('Employee Name :'); ?> <?php echo form_error('EmpName'); ?>
<?php echo form_input(array('id' => 'EmpName', 'name' => 'EmpName')); ?>
<?php echo form_label('Department :'); ?> <?php echo form_error('Department'); ?>
<?php echo form_input(array('id' => 'Department', 'name' => 'Department')); ?>
<?php echo form_label('LanID :'); ?> <?php echo form_error('LanID'); ?>
<?php echo form_input(array('id' => 'LanID', 'name' => 'LanID')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit'));?>
<?php echo form_close(); ?>
</div>
</body>
</html>
答案 0 :(得分:0)
在您的控制器中:
$data = array(
'Employee_Name' => $this->input->post('EmpName'),
'Department' => $this->input->post('Department'),
'LanID' => $this->input->post('LanID'),
);
所有数组索引是否与数据库表的列名完全匹配?
在您的模型中:
print_r($data);
之前:
$this->db->insert('requests', $data);
如果$ data包含数据,请尝试设置函数。
$this->db->set('Employee_Name', $EmpName);
$this->db->set('Department', $Department);
$this->db->set('LanID', $LanID);
$this->db->insert('requests');
尝试调试......