How to insert data into database with CodeIgniter

In this post, I will show inserting data into database table using CodeIgniter. We gonna use CodeIgniter Active Records Pattern. This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting.

let’s download and install the CodeIgniter as a first step and set your database configuration details in application/config/database.php file, if you need more information on database setup please refer my previous tutorial How to connect to MySQL database in CodeIgniter?

Here is my contact table schema, import it into your database.

CREATE TABLE `contact_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,
  `mobile` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `message` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `contact_table`
  ADD PRIMARY KEY (`id`);
 
ALTER TABLE `contact_table`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;COMMIT;

After CI application setup is ready, let’s create an HTML form to take the user inserts, we gonna insert user submitted data into the database table. Create a file called contact_form.php in the application/views/ directory with following code.

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

After creating from let’s create a Controller to handle user submitted data and to load view file when the user visits the page. We gonna create a controller called Contact.php file in the application/controllers.

load->model('contact_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 message 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 mobile number field $this->form_validation->set_rules('mobile', 'Mobile Number', 'required|regex_match[/^[0-9]{10}$/]'); // Validation rule for message field $this->form_validation->set_rules('message', 'Message', 'required|min_length[10]|max_length[50]'); if ($this->form_validation->run() == FALSE) { $this->load->view('contact_form'); } else { //Setting values for tabel columns $data = array( 'name' => $this->input->post('name'), 'email' => $this->input->post('email'), 'mobile' => $this->input->post('mobile'), 'message' => $this->input->post('message') ); //call the model method and pass data to it $this->insert_model->form_insert($data); //load the view $this->load->view('contact_form', $data); } } } ?>

final step create a model file which will interact with the database table, so let’s create a model called Contact_model.php file in application/models/ directory.

table = 'contact_table';
        }

        function form_insert($data){
            $this->db->insert($this->table, $data);
        }

    }

Now if you visit the page called http://localhost/contact you should get a contact form if user submits it that data will be inserted into the database table called contact_table.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status