CodeIgniter Insert Records into MySQL database Example

Codeigniter Mysql Mysqli php Programming

Today, We want to share with you CodeIgniter Insert Records into MySQL database Example.In this post we will show you insert form data into database using CodeIgniter, hear for how to insert data in database using model in CodeIgniter we will give you demo and example for implement.In this post, we will learn about how to insert post data in to database with example code in codeigniter with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

CodeIgniter Insert Records into MySQL database Example

There are the Following The simple About CodeIgniter Insert Records into MySQL database Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop php – insert data into database with codeigniter, so CodeIgniter save form data to database for this example is following below.

Step 1 : Import MySQL Table into your database

Create a member table schema

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

Step 2: CodeIgniter View Files


<div id="container">
    <?php echo form_open($this->uri->uri_string()); ?>
        <?php echo form_label('Member First Name :'); ?>
            echo form_input(array(
                'id' => 'memberfname',
                'name' => 'memberfname'
        <?php echo form_error('memberfname'); ?>
        <?php echo form_label('Email :'); ?> 
            echo form_input(array(
                'id' => 'email',
                'name' => 'email'
        <?php echo form_error('email'); ?>
        <?php echo form_label('Phone Number. :'); ?> 
            echo form_input(array(
                'id' => 'member_phone',
                'name' => 'member_phone'
        <?php echo form_error('member_phone'); ?>
        <?php echo form_label('Comments :'); ?> 
            echo form_input(array(
                'id' => 'member_cmnt',
                'name' => 'member_cmnt'
        <?php echo form_error('address'); ?>
            echo form_submit(array(
                'id' => 'submit',
                'value' => 'Submit'
    <?php echo form_close(); ?>

Setp 3 : Create a controllers in CodeIgniter


class Member extends CI_Controller {
    function __construct() {
    function index() {
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('memberfname', 'Member First Name', 'required|min_length[5]|max_length[15]');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('member_phone', 'Phone Number', 'required|regex_match[/^[0-9]{10}$/]');
        $this->form_validation->set_rules('member_cmnt', 'Comments', 'required|min_length[10]|max_length[50]');
        if ($this->form_validation->run() == FALSE) {
        } else {
            $membersdata = array(
                'memberfname' => $this->input->post('memberfname'),
                'email' => $this->input->post('email'),
                'member_phone' => $this->input->post('member_phone'),
                'member_cmnt' => $this->input->post('member_cmnt')

            $this->load->view('member_form', $membersdata);

Setp 4 : Create a Model in CodeIgniter


    class Member_model extends CI_Model {
        protected $table;
        function __construct() {
            $this->table = 'members_tbl';
        function form_insert($data){
            $this->db->insert($this->table, $data);

Angular 6 CRUD Operations Application Tutorials

Read :


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about CodeIgniter Insert Records into MySQL database Example.
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.

Related FAQ

Here are some more FAQ related to this Article:

  1.   Paytm Payment Gateway Integration in PHP step By step
  2.   Download Extract Zip Archives using PHP cURL
  3.   PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT
  4.   Shorthand comparisons using PHP Ternary Operator
  5.   How to Open URL in New Tab using Yii Framework
  6.   react open new tab with url Example Tutorial
  7.   crud with php oop and mvc design pattern
  8.   How to Force Download Files Using Laravel 5.7
  9.   Calling functions inside Vue.js template
  10.   PHP Laravel check Object is empty or not

Leave a Reply

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