How To Insert Data Using Codeigniter In Database.

In this articale I will explain you How To Insert Data Using Codeigniter In Database. CodeIgniter is an open source Web Application framework built in PHP designed to make your life as a programmer easier, while allowing you good speed for development, and also good performance when the site is up and running.Lets start and learn how to insert data in database using codeiginter.

Create table

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(12) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) NOT NULL,
  `email` varchar(64) NOT NULL,
  `user_type` enum('A','M','U','K') NOT NULL DEFAULT 'U',
  `user_department` varchar(50) NOT NULL,
  `profile_pic` varchar(64) NOT NULL,
  `status` enum('A','I') NOT NULL DEFAULT 'I',
  `creation_date` datetime NOT NULL,
  `activation_date` datetime DEFAULT NULL,
  `activation_code` varchar(64) DEFAULT NULL,
  `deactivation_date` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;
<html>

Create View add_user_view.php
location(codeginter/application/view)

<body>
 <form id="box-register" action="<?php echo site_url("add_user_controller/add_user"); ?>" method="post" enctype="multipart/form-data">
  <table class="zui-table zui-table-highlight-all"> 
  <tr>
  <td class="req"> name </td>
  <td ><p>
  <br/>
  <?php echo form_error('name', '<div class="error">', '</div>'); ?>
  <input class="form-control" type="text" name="name" value="<?php echo set_value('name'); ?>" id="name" required="required"/>
  </p>
  </td>
  </tr>
  <tr>
  <td class="req"> Email </td>
  <td>
  <p>
  <br/>
  <?php echo form_error('email', '<div class="error">', '</div>'); ?>
  <input class="form-control" type="email" name="email" value="" id="email" required="required"/>
  </p>
  </td>
  </tr>
  <tr>
  <td class="req"> User Type </td>
  <td>
  <p>
  <br/>
  <select class="form-control" name="user_type" id="user_type">
  <option value="U">User</option>
  <option value="M">Manager</option>  </select>
  </p>
  </td>
  </tr>
  <tr>
  <td class="req"> Department </td>
  <td>
  <p>
  <br/>
  <select class="form-control" name="user_department" id="user_department">
  <option value="Marketing">Marketing</option>
  <option value="PHP">PHP Developer</option>
  <option value="SEO">SEO</option>
  <option value="Web Designer">Web Designer</option>
  </select>
  </p>
  </td>
  </tr>
  <tr>
 <td></td>
  <td>
  <p class="fr">
  <input type="submit" name="submit" class="btn btn-primary" value="Register" id="register"/>
  </p>
  </td>
  </tr>
 <tr>
  <a class="forgot" href="<?php echo site_url('project'); ?>"> Back to Home Page </a> 
 </tr>
  </table>
  <div class="clear"></div>
  </form>
  </body>
  </html>

Controller: now create controller Add_user_controller.php file in controller folder.
location(codeginter/application/controller)

if (!defined('BASEPATH'))
  exit('No direct script access allowed');
  class Add_user_controller extends CI_Controller {
  public function __construct() {
  parent::__construct();
  $this->load->model('add_user_model');
  }
  function add_user()
  {
 $username = $this->input->post('name');
  $email = $this->input->post('email');
  $user_type = $this->input->post('user_type');
  $user_department = $this->input->post('user_department');
  $creation_date = date('Y-m-d H:i:s');
  $activation_date = date('Y-m-d H:i:s');
  $user_data = array(
  'name' => $username,
  'email' => $email,
  'pwd' => $password,
  'user_type' => $user_type,
  'user_department' => $user_department,
  'status'=>'A',
  'creation_date' => $creation_date, 
  'activation_date'=>$activation_date
  );
  $this->add_user_model->add_user($data);
  }
  }

Create Model: now create model Add_user_model.php file in model folder.
location(codeginter/application/model)
 
 if (!defined('BASEPATH'))
  exit('No direct script access allowed');
 class User_model extends CI_Model {
  public function __construct() {
  parent::__construct();
  }
  function add_user($data){
 
  $this->db->insert('users', $data); 
  echo "user Added";
  }
  }

Hope this will help You. Thanks keep reading.