2015-02-06 107 views
0

我有一個簡單的表單設置。它顯示了很好,當我點擊提交看起來像它正在工作,但是當我去檢查MSSQL服務器中的數據它不在那裏。我不確定它爲什麼不起作用。codeigniter表單數據沒有插入MSSQL數據庫

控制器是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); 
} 
} 
?> 

View是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

你嘗試過調試?

在你的控制器:

$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); 

如果$數據中包含的數據,儘量設置功能。

$this->db->set('Employee_Name', $EmpName); 
$this->db->set('Department', $Department); 
$this->db->set('LanID', $LanID); 
$this->db->insert('requests'); 

嘗試調試......

+0

你是對TY我努力學習,我需要學習更多的調試。我不擅長調試我可能需要在調試時需要一個類 – Donny 2015-02-06 23:15:27

+0

不要弄錯......我正在調試一個小問題近30個小時......終於找到了這個小...這是最糟糕的事情在世界上.... – Shuhail 2015-02-06 23:18:44