How to insert data into database with CodeIgniter?

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.

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`
  ADD PRIMARY KEY (`id`);
 
ALTER TABLE `phonebook_table`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;COMMIT;

Step 2: Insert & submitted Data

Insert user submitted data into the database table

application/views/phonebook_form.php

Read Also:  how to fetch single data from database in codeigniter?

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

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

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

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

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

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

</div>

Step 3: create a Controller in codeigniter

application/controllers/Phonebook.php

<?php

class Phonebook extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('phonebook_model');
        $this->load->model(array('form','url','html'));
        $this->load->databae();
    }

    function index() {

        //laod CI's validation library
        $this->load->library('form_validation');

        // 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) {

            $this->load->view('phonebook_form');

        } 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
            $this->insert_model->form_insert($members_info);

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

}

?>

Step : 4 create a model

application/models/Phonebook_model.php

<?php
    class Phonebook_model extends CI_Model {

        protected $table;

        function __construct() {
            parent::__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 infinityknow.com 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.