How to insert data into database in CodeIgniter – [Example]

Published 03/10/2021 16:13 | CodeIgniter | comments
Rate this post

Here we discuss how to Insert Post data in to the database with example code in CodeIgniter. This code you can use In your project to insert data in the MySQL database. All data submission projects must need MySQL query to submit it.

How to insert data into database in CodeIgniter

In this example we are going to describe and how you can create a table in data base. Read and apply the below code for insert code.

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

First of all, you need to create a table in Codeigniter insert data into database SQL so use this query:

CREATE TABLE crud (
`id` int(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL
);

In this project we use 3 types of files for data in MySQL:

  • Crud.php Path: codeIgniter\application\controllers\Crud.php
  • Crud_model.php Path: codeIgniter\application\models\Crud_model.php
  • insert.php Path: codeIgniter\application\views\insert.php

Crud.php (Controller)

After doing all steps need to run the program using URL:

http://localhost/codeIgniter/index.php/Crud/savedata

[wpdevart_facebook_comment order_type="social" title_text="" title_text_color="#000000" title_text_font_size="22" title_text_font_famely="Open Sans" title_text_position="left" width="100%" bg_color="#CCCCCC" animation_effect="random" count_of_comments="2" ]