Search code examples
phpsql-servercodeigniter-2

codeigniter form data is not inserting into MSSQL database


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> 

Solution

  • 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...