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`
ALTER TABLE `phonebook_table`

Step 2: Insert & submitted Data

Insert user submitted data into the database table


uri->uri_string()); ?> 'name', 'name' => 'name' )); ?> 'email', 'name' => 'email' )); ?> 'phone', 'name' => 'phone' )); ?> 'imp_notes', 'name' => 'imp_notes' )); ?> 'submit', 'value' => 'Submit' )); ?>

Step 3: create a Controller in codeigniter



    function index() {

        //laod CI's validation library

        // set validation error imp_notes delimiter
', '
'); // 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


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.

Leave a Comment