How to insert data into database with CodeIgniter?

Rate this post

Today, We want to share with you how to insert data into database in codeigniter.In this post we will show you insert and display data from database in codeigniter, hear for insert data in codeigniter using ajax we will give you demo and example for implement.In this post, we will learn about Insert Query In Codeigniter Example Tutorial with an example.

insert query in codeigniter example

This pattern allows all the details to be retrieved(select), inserted, and updated in your mysql database with tiny scripting.

Free Live Chat for Any Issue

Step 1: set your database configuration details

application/config/database.php File

my phonebook table schema, import it into your database.

CREATE TABLE `phonebook_table` (
  `id` int(10) UNSIGNED NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `phone` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `imp_notes` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `phonebook_table`
ALTER TABLE `phonebook_table`

Step 2: Insert & submitted Data

Insert user submitted data into the database table

Read Also:  mysql order by rand - Best 5 Ways To Get MySQL Select Random Records


<div id="container">
	<?php echo form_open($this->uri->uri_string()); ?>

		<?php echo form_label('Name :'); ?>
			echo form_input(array(
				'id' => 'name',
				'name' => 'name'
		<?php echo form_error('Member name'); ?>

		<?php echo form_label('Member Email :'); ?> 
			echo form_input(array(
				'id' => 'email',
				'name' => 'email'
		<?php echo form_error('email'); ?>

		<?php echo form_label('Member Phone Number. :'); ?> 
			echo form_input(array(
				'id' => 'phone',
				'name' => 'phone'
		<?php echo form_error('phone'); ?>

		<?php echo form_label('Member Imp Notes :'); ?> 
			echo form_input(array(
				'id' => 'imp_notes',
				'name' => 'imp_notes'
		<?php echo form_error('address'); ?>

			echo form_submit(array(
				'id' => 'submit',
				'value' => 'Submit'
	<?php echo form_close(); ?>


Step 3: create a Controller in codeigniter



class Phonebook extends CI_Controller {

    function __construct() {

    function index() {

        //laod CI's validation library

        // set validation error imp_notes delimiter
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        // Validation rule for name field
        $this->form_validation->set_rules('name', 'Name', 'required|min_length[5]|max_length[15]');

        // Validation rule for email field
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');

        // Validation rule for phone number field
        $this->form_validation->set_rules('phone', 'Mobile Number', 'required|regex_match[/^[0-9]{10}$/]');

        // Validation rule for imp_notes field
        $this->form_validation->set_rules('imp_notes', 'Message', 'required|min_length[10]|max_length[50]');

        if ($this->form_validation->run() == FALSE) {


        } else {

           //Setting values for tabel columns
            $members_info = array(
                'name' => $this->input->post('name'),
                'email' => $this->input->post('email'),
                'phone' => $this->input->post('phone'),
                'imp_notes' => $this->input->post('imp_notes')

            //call the model method and pass members_info to it

            //load the view
            $this->load->view('phonebook_form', $members_info);



Step : 4 create a model


    class Phonebook_model extends CI_Model {

        protected $table;

        function __construct() {
            $this->table = 'phonebook_table';

        function form_insert($data){
            $this->db->insert($this->table, $data);


Now, Last step if you visit the page called http://localhost/phonebook you should get a phonebook form if user submits it that all the data will be inserted into the mysql database table called phonebook_table.


I hope you get an idea about how to insert data into database in codeigniter.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.