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

Step 2: CodeIgniter View Files

application/views/member_form.php

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

Setp 3 : Create a controllers in CodeIgniter

application/controllers/Member.php

<?php
 
class Member extends CI_Controller {
 
    function __construct() {
        parent::__construct();
        $this->load->model('member_model');
        $this->load->model(array('form','url','html'));
        $this->load->databae();
    }
 
    function index() {
 
        $this->load->library('form_validation');
        $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) {
 
            $this->load->view('member_form');
 
        } 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->insert_model->form_insert($membersdata);
            $this->load->view('member_form', $membersdata);
        }
    }
 
}
 
?>

Setp 4 : Create a Model in CodeIgniter

application/models/Member_model.php

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

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

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 Pakainfo.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.


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 *