{"id":2637,"date":"2018-10-05T20:59:51","date_gmt":"2018-10-05T15:29:51","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2637"},"modified":"2026-09-28T20:40:01","modified_gmt":"2026-09-28T15:10:01","slug":"insert-data-into-database-using-codeigniter","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/insert-data-into-database-using-codeigniter\/","title":{"rendered":"Insert data into database using codeigniter"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong>Post Date: <\/strong>October 04, <strong>2018<\/strong><br><strong>Last Updated:<\/strong> September 25, 2026<\/h4>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">CodeIgniter is a lightweight PHP framework that follows the MVC (Model-View-Controller) architecture. One of the most common tasks in web application development is inserting user data into a database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, you will learn how to create a simple registration form and store user information such as name, email, and mobile number into a MySQL database using CodeIgniter. We will use a Controller, View, and Model to keep the application organized and maintainable.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, ensure that:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CodeIgniter is installed and configured.<\/li>\n\n\n\n<li>A MySQL database connection is configured in <code>application\/config\/database.php<\/code>.<\/li>\n\n\n\n<li>A <code>users<\/code> table exists in your database.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Users Table<\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    name VARCHAR(100),\n    email VARCHAR(100),\n    mobile VARCHAR(20)\n);<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Application Flow<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">User Opens Registration Form\n            \u2502\n            \u25bc\n      Fill Details\n(Name, Email, Mobile)\n            \u2502\n            \u25bc\n      Click Save Data\n            \u2502\n            \u25bc\n Controller (Insert.php)\n            \u2502\n            \u25bc\n  Model (Insert_Model.php)\n            \u2502\n            \u25bc\n Insert Record into Database\n            \u2502\n            \u25bc\n Success Message Displayed<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 1: Create Controller<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">application\/controllers\/Insert.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nclass Insert extends CI_Controller\n{\n    public function __construct()\n    {\n        parent::__construct();\n\n        \/\/ Load database\n        $this->load->database();\n\n        \/\/ Load model\n        $this->load->model('Insert_Model');\n    }\n\n    public function savedata()\n    {\n        \/\/ Load registration form\n        $this->load->view('registration');\n\n        if($this->input->post('save'))\n        {\n            $n = $this->input->post('name');\n            $e = $this->input->post('email');\n            $m = $this->input->post('mobile');\n\n            $this->Insert_Model->saverecords($n, $e, $m);\n\n            echo \"Records Saved Successfully\";\n        }\n    }\n}\n?><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 2: Create Registration View<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">application\/views\/registration.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;!DOCTYPE html>\n&lt;html>\n&lt;head>\n    &lt;title>Registration Form&lt;\/title>\n&lt;\/head>\n\n&lt;body>\n\n&lt;form method=\"post\">\n\n&lt;table width=\"600\" border=\"1\" cellspacing=\"5\" cellpadding=\"5\">\n\n&lt;tr>\n&lt;td width=\"230\">Enter Your Name&lt;\/td>\n&lt;td width=\"329\">\n&lt;input type=\"text\" name=\"name\"\/>\n&lt;\/td>\n&lt;\/tr>\n\n&lt;tr>\n&lt;td>Enter Your Email&lt;\/td>\n&lt;td>\n&lt;input type=\"text\" name=\"email\"\/>\n&lt;\/td>\n&lt;\/tr>\n\n&lt;tr>\n&lt;td>Enter Your Mobile&lt;\/td>\n&lt;td>\n&lt;input type=\"text\" name=\"mobile\"\/>\n&lt;\/td>\n&lt;\/tr>\n\n&lt;tr>\n&lt;td colspan=\"2\" align=\"center\">\n&lt;input type=\"submit\" name=\"save\" value=\"Save Data\"\/>\n&lt;\/td>\n&lt;\/tr>\n\n&lt;\/table>\n\n&lt;\/form>\n\n&lt;\/body>\n&lt;\/html><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Step 3: Create Model<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">application\/models\/Insert_Model.php<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nclass Insert_Model extends CI_Model\n{\n    function saverecords($name, $email, $mobile)\n    {\n        $query = \"INSERT INTO users VALUES('', '$name', '$email', '$mobile')\";\n        $this->db->query($query);\n    }\n}\n?><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Testing the Application<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Start your web server (Apache\/Nginx).<\/li>\n\n\n\n<li>Open your browser.<\/li>\n\n\n\n<li>Navigate to:<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/localhost\/project-name\/index.php\/insert\/savedata<\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>Fill in the registration form.<\/li>\n\n\n\n<li>Click <strong>Save Data<\/strong>.<\/li>\n\n\n\n<li>Verify that the record is successfully inserted into the <code>users<\/code> table.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Security Recommendation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The above example is intended for learning purposes. In production environments, avoid using raw SQL queries because they are vulnerable to SQL Injection attacks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, use CodeIgniter&#8217;s Query Builder:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$data = array(\n    'name'   => $name,\n    'email'  => $email,\n    'mobile' => $mobile\n);\n\n$this->db->insert('users', $data);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This approach is safer, cleaner, and easier to maintain.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, you learned how to insert data into a MySQL database using CodeIgniter&#8217;s MVC architecture. We created a controller to handle user requests, a view to display the registration form, and a model to perform database operations. While the example uses a simple SQL query for demonstration, production applications should always use CodeIgniter&#8217;s Query Builder and form validation features to improve security, reliability, and maintainability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By following these practices, you can build secure and scalable CodeIgniter applications with clean and organized code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions (FAQ)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Why should I use a Model for database operations in CodeIgniter?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Models help separate database logic from application logic. This follows the MVC architecture and makes your code easier to maintain, test, and reuse.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Can I validate form fields before inserting data?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. CodeIgniter provides a Form Validation Library that allows you to validate required fields, email formats, mobile numbers, and more before saving data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the safest way to insert data into a database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The recommended method is to use CodeIgniter&#8217;s Query Builder or Active Record methods instead of writing raw SQL queries. This helps protect against SQL Injection vulnerabilities.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/restore-database-on-xampp-from-command-line\/\">Restore Database on Xampp from command line<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-admin\/post.php?post=1634&amp;action=edit\">Export data from Database to Excel Sheet(.xls) using Codeigniter<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/create-a-database-and-the-user-for-that-database-in-cpanel\/\">Create a database and the user for that database in cPanel<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have a technology challenge or looking for the right solution for your business? Our team can help you with <strong>cloud, DevOps, development, infrastructure, design, and more<\/strong>. Feel free to reach out to our experts <a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: October 04, 2018Last Updated: September 25, 2026 Introduction CodeIgniter is a lightweight PHP framework that follows the MVC [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[341,179,273],"class_list":["post-2637","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeigniter","tag-mysql-2","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-Gx","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2637"}],"version-history":[{"count":2,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2637\/revisions"}],"predecessor-version":[{"id":11992,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2637\/revisions\/11992"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}