codeigniter表单数据未插入MSSQL数据库(codeigniter form data is not inserting into MSSQL database)

编程入门 行业动态 更新时间:2024-10-24 04:35:25
codeigniter表单数据未插入MSSQL数据库(codeigniter form data is not inserting into MSSQL database)

我有一个简单的表单设置。 它显示正常,当我点击提交看起来它正在工作,但当我去检查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>

I have a simple form setup. It shows up fine and when I hit submit looks like it is working, but when I go to check the datat in MSSQL Server it is not there. I am not sure why it is not working.

Controller is 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'); } } } ?>

Model is 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 is 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>

最满意答案

你试过调试吗?

在你的控制器中:

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

试试调试......

have you tried debugging?

in your controller:

$data = array( 'Employee_Name' => $this->input->post('EmpName'), 'Department' => $this->input->post('Department'), 'LanID' => $this->input->post('LanID'), );

does all array index matches exactly with your database table's column name?

in your model:

print_r($data);

before:

$this->db->insert('requests', $data);

if $data contains data, try set function.

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

try debugging...

更多推荐

本文发布于:2023-07-15 21:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据库   数据   MSSQL   codeigniter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!