2016-12-20 3 views
0

Я пытаюсь сделать функцию вставки в codeigniter, используя php и bootstrap. Но когда я нажимаю кнопку добавления, ничего не происходит, мои новые данные не добавляются в мою таблицу db. Пожалуйста помогите. Мой код:Вставить данные в codeigniter

department.php

<?php 
/* 
* File Name: employee.php 
*/ 
if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class department extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->library('session'); 
     $this->load->helper('form'); 
     $this->load->helper('url'); 
     $this->load->database(); 
     $this->load->library('form_validation'); 
     //load the employee model 
     $this->load->model('department_model'); 
    } 

    //index function 
    function index() 
    { 
     //fetch data from department and designation tables 
     // $data['department'] = $this->department_model->get_department(); 

     //set validation rules 
     $this->form_validation->set_rules('id', 'Employee ID', 'trim|required|numeric'); 
     $this->form_validation->set_rules('department_emer', 'Department Name', 'trim|required|callback_alpha_only_space'); 
     $this->form_validation->set_rules('pershkrimi', 'Description', 'trim|required'); 

     //$this->form_validation->set_rules('id_departament', 'Department', 'callback_combo_check'); 



     if ($this->form_validation->run() == FALSE) 
     { 
      //fail validation 
      $this->load->view('department_view'); 
     } 
     else 
     {  
      //pass validation 
      $data = array(
       //'id' => $this->input->post('id'), 

       'department_emer' => $this->input->post('department_emer'), 
       'pershkrimi' => $this->input->post('pershkrimi'), 

      ); 

      //insert the form data into database 
      $this->db->insert('department', $data); 

      //display success message 
      $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">Department details added to Database!!!</div>'); 
      redirect('department/index'); 
     } 

    } 



    //custom validation function to accept only alpha and space input 
    function alpha_only_space($str) 
    { 
     if (!preg_match("/^([-a-z ])+$/i", $str)) 
     { 
      $this->form_validation->set_message('alpha_only_space', 'The %s field must contain only alphabets or spaces'); 
      return FALSE; 
     } 
     else 
     { 
      return TRUE; 
     } 
    } 
} 
?> 

department_model.php

<?php 
/* 
* File Name: employee_model.php 
*/ 
if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class department_model extends CI_Model 
{ 
    function __construct() 
    { 
     // Call the Model constructor 
     parent::__construct(); 
    } 

    //get department table to populate the department name dropdown 



} 
?> 

department_view.php

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title>CodeIgniter | Insert Employee Details into MySQL Database</title> 
    <!--link the bootstrap css file--> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
    <!-- link jquery ui css--> 
    <link href="<?php echo base_url('assets/jquery-ui-1.11.2/jquery-ui.min.css'); ?>" rel="stylesheet" type="text/css" /> 
    <!--include jquery library--> 
    <script src="<?php echo base_url('assets/js/jquery-1.10.2.js'); ?>"></script> 
    <!--load jquery ui js file--> 
    <script src="<?php echo base_url('assets/jquery-ui-1.11.2/jquery-ui.min.js'); ?>"></script> 

    <style type="text/css"> 
    .colbox { 
     margin-left: 0px; 
     margin-right: 0px; 
    } 
    </style> 

    <script type="text/javascript"> 
    //load datepicker control onfocus 
    $(function() { 
     $("#hireddate").datepicker(); 
    }); 
    </script> 

</head> 
<body> 
<div class="container"> 
    <div class="row"> 
     <div class="col-sm-offset-3 col-lg-6 col-sm-6 well"> 
     <legend>Add Department Details</legend> 
     <?php 
     $attributes = array("class" => "form-horizontal", "id" => "departmentform", "name" => "departmentform"); 
     echo form_open("department/index", $attributes);?> 
     <fieldset> 


      <div class="form-group"> 
      <div class="row colbox"> 
      <div class="col-lg-4 col-sm-4"> 
       <label for="department_emer" class="control-label">Name</label> 
      </div> 
      <div class="col-lg-8 col-sm-8"> 
       <input id="department_emer" name="department_emer" placeholder="department_emer" type="text" class="form-control" value="<?php echo set_value('department_emer'); ?>" /> 
       <span class="text-danger"><?php echo form_error('department_emer'); ?></span> 
      </div> 
      </div> 
      </div> 
      <div class="form-group"> 
      <div class="row colbox"> 
      <div class="col-lg-4 col-sm-4"> 
       <label for="mbiemer" class="control-label">Description</label> 
      </div> 
      <div class="col-lg-8 col-sm-8"> 
       <textarea class="form-control" rows="5" id="pershkrimi" name="pershkrimi" placeholder="pershkrimi" value="<?php echo set_value('pershkrimi'); ?>" ></textarea> 
       <span class="text-danger"><?php echo form_error('pershkrimi'); ?></span> 
      </div> 
      </div> 
      </div> 

      <div class="form-group"> 
      <div class="col-sm-offset-4 col-md-8 text-left"> 
       <input id="btn_update" name="btn_update" type="submit" class="btn btn-primary" value="Add" /> 
       <input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel" /> 
      </div> 
      </div> 
     </fieldset> 
     <?php echo form_close(); ?> 
     <?php echo $this->session->flashdata('msg'); ?> 
     </div> 
    </div> 
</div> 
</body> 
</html> 

ответ

0

В department.php заменить

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

с

$this->load->model('department_model.php'); 
$this->department_model.php->insert($data); 

И в department_model.php создать функцию вставки

public function insert($data) { 

    if($this->db->insert('table_name', $data)) { 

     //Success message or anything which you want 

    } 
} 

Это позволит решить вашу проблему.

Дайте мне знать, если вам нужна дополнительная помощь !!!

0

Загрузите библиотеку базы данных CI перед вставкой данных или вы можете загрузить ее в свой конструктор.

 $this->load->library('database'); 

Надеюсь, это может вам помочь.