How to insert data into database with CodeIgniter

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

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.

<div id="container">
    <?php echo form_open($this->uri->uri_string()); ?>
 
        <?php echo form_label('Name :'); ?>
        <?php
            echo form_input(array(
                'id' => 'name',
                'name' => 'name'
            ));
        ?>
        <?php echo form_error('name'); ?>
 
        <?php echo form_label('Email :'); ?>
        <?php
            echo form_input(array(
                'id' => 'email',
                'name' => 'email'
            )

Share This Blog

Comment

Do you want to get our quality service for your business?