Last updated on November 18, 2022
In this post, I will show inserting data into a 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 to 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 the CI application setup is ready, let’s create an HTML form to take the user inserts, we gonna insert user-submitted data into the table. Create a file called contact_form.php in the application/views/ directory with the following code.
<?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' )); ?>
<?php echo form_error('email'); ?>
<?php echo form_label('Mobile Number. :'); ?>
<?php echo form_input(array('id' => 'mobile', 'name' => 'mobile' )); ?>
<?php echo form_error('mobile'); ?> <?php echo form_label('Message :'); ?>
<?php echo form_input(array('id' => 'message', 'name' => 'message' )); ?>
<?php echo form_error('address'); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit' )); ?>
<?php echo form_close(); ?>
After creating from let’s create a Controller to handle user-submitted data and to load the view file when the user visits the page. We gonna create a controller called Contact.php
file in the application/controllers
<?php
class Contact extends CI_Controller {
function __construct() {
parent::__construct();
$this->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('<pre><div class="error">', '</div></pre>');
// 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);
}
}
}
In the final step, let’s create a model file that will interact with the database table, so let’s create a model called Contact_model.php
file in application/models/
directory.
<?php
class Contact_model extends CI_Model {
protected $table;
function __construct() {
parent::__construct();
$this->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 the user submits it that data will be inserted into the table called contact_table
.