Codeigniter – Insert

Codeigniter – Insert

This tutorial will show you how to add record to database using Codeigniter.

I will use Codeigniter’s Active Record for database queries which is a safer way to execute queries than raw SQL.

Before we begin with Codeigniter Create Tutorial, you need to set up your Codeigniter.

Setting up CodeIgniter Framework

Also, you need to configure your CodeIgniter for this tutorial.

CodeIgniter Configuration

Lets start with our first lesson and that is inserting data to database.

Controllers are used for communication between clients and server.

Communication looks like this:

Client send request via web browser to controller.

Controller than passes user’s request to model or view, depends what’s the purpose of that controller.

After that, controller sends result to client.

That’s called MVC pattern, and it’s a very common in programming languages.

After some details about CodeIgniter principles, we can continue with our tutorial.



I will add models, helpers and other stuff to autoload.php because it’s a little easier in case you forget to include some of that in controller’s functions, this way you can avoid possible errors.

Open autoload.php in application/config directory and find these lines

And make sure they look like this

Now that we have loaded everything we need, we can proceed with creating controller.



Create new php file in application/controllers folder, we can name it Site, this will be our main controller for now.

I will use CodeIgniter’s built in Form Validation which you can use to ensure that user inputs only value that is allowed to avoid possible errors.


If we want to make this controller default controller for our application, we need to edit routes.php file in application/config directory

Find line

Replace welcome with our controller name site

Now, when we visit our CodeIgniter webpage, it will load our Site controller.

Next thing to do is to add new route in routes.php

This means that when we type

it will be understood as

This is optional, but can make your URLs a lot more user friendly.



Create new model Component that we will use for communication with database, models are located in application/models directory


Model now contains only function for inserting data to database, we’ll expand it in next tutorials.



We need to create view files that we’ll use for header and footer so we can include them in the rest of our views.

Create new directory in application/views folder, name it templates.

Add two PHP files inside templates folder



Now, we will create two more view files, one we’ll use in this tutorial, and another will be index page that will show all data from database which will be next tutorial.

Create files site.php and add_component.php in application/views directory



Now, open your browser and visit your CodeIgniter page, it should look like this

CodeIgniter Site

Click on Options > New Component and it will load our page for adding new component

CodeIgniter Add Page

Fill out the form and you should be redirected to Hardware Components page.

You can now go to phpMyAdmin and check if data is inserted in database.

If data is successfully inserted, you should see it in components table

CodeIgniter Add Database

If there are no new rows in table, you shoul check your code for possible errors.

Our first lesson – CodeIgniter Insert is now complete, stay tuned for our next tutorial where we’ll be reading data from database and display it on page.

You can download complete source code from this tutorial on the link bellow

CodeIgniter Lesson 1 – Insert

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *