Data Insertion in CodeIgniter

In this tutorial we will learn about how to insert data into database in codeIgniter.
1.Create a database then configure your db credential in application/database.php

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost', // your hostname
	'username' => 'root', // your DB username
	'password' => '',     // DB password
	'database' => 'cidb', // your database name
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE

2. load some libraries and helpers in application/autoload.php

$autoload['libraries'] = array('database','session');
$autoload['helper'] = array('html','form');
  • Database library used for database related queries
  • Session library  used for display success / error message using flashdata.
  • Html Helper for used for link_tag
  • form helper used for form

First create a  view(registration.php) inside application/view folder

<?php echo form_open('registration',['name'=>'registration'])?>
<!---- Success Message ---->
<?php if ($this->session->flashdata('success')) { ?>
 <p style="color:red; font-size:16px; "><?php echo $this->session->flashdata('success'); ?></p>
<?php } ?>
<!---- Error Message ---->
<?php if ($this->session->flashdata('error')) { ?>
<p style="color:red; font-size:16px;"><?php echo $this->session->flashdata('error'); ?></p>
<?php } ?>
<?php echo form_label('Enter your Name', 'name'); ?>
<?php echo form_input(['name'=>'fullname','id'=>'fullname','placeholder'=>'Enter you full name','value'=>set_value('fullname')]);?>
<?php echo form_error('fullname',"<div style='color:red'>","</div>");?>
<?php echo form_label('Enter your Contact Mobile Number', 'mobnumber'); ?>
<?php echo form_input(['name'=>'mobnumber','id'=>'mobnumber','placeholder'=>'Enter your Contact Mobile Number','value'=>set_value('mobnumber')]);?>
<?php echo form_error('mobnumber',"<div style='color:red'>","</div>");?>
<?php echo form_label('Enter your Valid Email id', 'emailid'); ?>
<?php echo form_input(['name'=>'emailid','id'=>'emailid','placeholder'=>'Enter your Valid Email id','value'=>set_value('emailid')]);?>
<?php echo form_error('emailid',"<div style='color:red'>","</div>");?>
<?php echo form_label('Select gender','gender'); ?> :
echo form_dropdown('gender',$options);?>
<?php echo form_label('Enter your address','address'); ?> <br />
<?php echo form_textarea(['name'=>'address','rows'=>'5','cols'=>'60','id'=>'address','class'=>'form-control','placeholder'=>'Enter your address','value'=>set_value('address')]);?>
<?php echo form_error('address',"<div style='color:red'>","</div>");?>
<p class="termcondtion">
<?php echo form_checkbox('TermsCondition','accept', TRUE); ?>
<?php echo form_label('Accept terms and conditions','terms'); ?>
<p class="login button">
<?php echo form_submit(['name'=>'submit','value'=>'Submit']);?>
<?php echo form_close();?>

Now create a controller (Registration.php) inside application/controller. You can also set your default controller in application/routes.php

$route['default_controller'] = 'Registration';
Class Registration extends CI_Controller{
public function index(){
//library form validation . you can also load this libray in application/autoload.php
$this->form_validation->set_rules('fullname','Full Name','required');
$this->form_validation->set_rules('mobnumber','Mobile Number','required|numeric|exact_length[10]');
$this->form_validation->set_rules('emailid','Email id','required|valid_email');

Create a model (Registration_Model.php) inside application/model.

Class Registration_Model extends CI_Model{
public function insertdata($fname,$mnumber,$emaild,$gender,$adress,$tc){
$this->session->set_flashdata('success', 'Registration successful');
$this->session->set_flashdata('error', 'Somthing went worng. Error!!');

SQL Table structure for tblusers

CREATE TABLE `tblusers` (
  `id` int(11) NOT NULL,
  `fullName` varchar(150) DEFAULT NULL,
  `mobileNumber` char(12) DEFAULT NULL,
  `emailId` varchar(50) DEFAULT NULL,
  `gender` varchar(30) DEFAULT NULL,
  `address` mediumtext,
  `termsCondition` varchar(200) DEFAULT NULL,
  `postingDate` timestamp NULL DEFAULT CURRENT_TIMESTAMP

View Demo———————————————–
View Demo

How to run this script

  1. Download the zip file .
  2. Extract the zip and copy registrationci folder
  3. Paste in root directory(For xampp htdocs and for wamp www)
  4. Open your browser put http://localhost/phpmyadmin
  5. Create a database cidb
  6. Import sql file tblusers.sql
  7. Run your script http://localhost/registrationci

Data Insertion in CodeIgniter (Download full source code)
Size: 2.76 MB
Version: v 1

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.

Recommended Tutorials for you

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.