Posted inphp / Codeigniter

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

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

application/controllers/Phonebook.php

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('
', '
'); // 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

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype