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.

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.

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.

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.

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.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status