CodeIgniter Tutorial: How to Insert Data into Database using CodeIgniter

This video learn how to insert data into database using CodeIgniter



1- Update database connection with file database.php in config folder

2- Update line of code with autoload.php in config folder

   Before

   $autoload['libraries'] = array();

   After

   $autoload['libraries'] = array('database');

3- Go MyPhpAdmin to create database

   Example: (Database name: college)

   RUN SQL

   CREATE DATABASE college;

   CREATE TABLE students(
Student_id int(10) NOT NULL AUTO_INCREMENT,
Student_Name varchar(255) NOT NULL,
Student_Email varchar(255) NOT NULL,
Student_Mobile varchar(255) NOT NULL,
Student_Address varchar(255) NOT NULL,
PRIMARY KEY (Student_id)
   )

4- Create file styles.css

#container {
width:960px;
height:610px;
margin:50px auto
}
.error {
color:red;
font-size:13px;
margin-bottom:-15px
}
form {
width:345px;
padding:0 50px 20px;
background:linear-gradient(#fff,#FF94BB);
border:1px solid #ccc;
box-shadow:0 0 5px;
font-family:'Marcellus',serif;
float:left;
margin-top:10px
}
h1 {
text-align:center;
font-size:28px
}
hr {
border:0;
border-bottom:1.5px solid #ccc;
margin-top:-10px;
margin-bottom:30px
}
label {
font-size:17px
}
input {
width:100%;
padding:10px;
margin:6px 0 20px;
border:none;
box-shadow:0 0 5px
}
input#submit {
margin-top:20px;
font-size:18px;
background:linear-gradient(#22abe9 5%,#36caf0 100%);
border:1px solid #0F799E;
color:#fff;
font-weight:700;
cursor:pointer;
text-shadow:0 1px 0 #13506D
}
input#submit:hover {
background:linear-gradient(#36caf0 5%,#22abe9 100%)
}

Save this file to "C:\xampp\htdocs\xampp\CodeIgniter\css"

5- Create file insert_view.php for VIEW

<html>
<head>
<title>Insert Data Into Database Using CodeIgniter Form</title>
<link href='http://fonts.googleapis.com/css?family=Marcellus' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="http://localhost/xampp/CodeIgniter/css/styles.css"/>
</head>
<body>

<div id="container">
<!-- Open Controllers of insert_ctrl -->
<?php echo form_open('insert_ctrl'); ?>

<h1>Insert Data Into Database Using CodeIgniter</h1>
<!-- Create Form Register Student to Save Table Students of database College -->

<!-- Label Name of Student Name -->
<?php echo form_label('Student Name :'); ?>
<!-- Validation error when user input not correct condition -->
<?php echo form_error('dname'); ?>
<!-- Textbox control for input name of student with field Student_Name -->
<?php echo form_input(array('id' => 'dname', 'name' => 'dname')); ?>

<?php echo form_label('Student Email :'); ?> <?php echo form_error('demail'); ?>
<?php echo form_input(array('id' => 'demail', 'name' => 'demail')); ?>

<?php echo form_label('Student Mobile No. :'); ?> <?php echo form_error('dmobile'); ?>
<?php echo form_input(array('id' => 'dmobile', 'name' => 'dmobile','placeholder'=>'10 Digit Mobile No.')); ?>

<?php echo form_label('Student Address :'); ?> <?php echo form_error('daddress'); ?>
<?php echo form_input(array('id' => 'daddress', 'name' => 'daddress')); ?>

<!-- Button Submit -->
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit'));?>

<!-- Close Controllers -->
<?php echo form_close(); ?>

</div>

</body>
</html>

Save this file to "C:\xampp\htdocs\xampp\CodeIgniter\application\views"

6- Create file insert_ctrl for CONTROLLERS

<?php

class insert_ctrl extends CI_Controller {
function __construct() {
parent::__construct();
//This line to load model of insert_model
$this->load->model('insert_model');
}
//index function to load form validation and get data from form view submit
function index()
{

// Including Validation Library
$this->load->library('form_validation');

$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

// Validating Name Field is require minimum 5 characters and maximum 15 characters
$this->form_validation->set_rules('dname', 'Username', 'required|min_length[5]|max_length[15]');

// Validating Email Field is require valid mail address
$this->form_validation->set_rules('demail', 'Email', 'required|valid_email');

// Validating Mobile no. Field is require mobile no format
$this->form_validation->set_rules('dmobile', 'Mobile No.', 'required|regex_match[/^[0-9]{10}$/]');

// Validating Address Field minimum 10 characters and maximum 50 characters
$this->form_validation->set_rules('daddress', 'Address', 'required|min_length[10]|max_length[50]');

//This condition to check validation when false please retype again if true go save data
//into fields Student_Name, Student_Email, Student_Mobile, Student_Address
if ($this->form_validation->run() == FALSE)
{
$this->load->view('insert_view');
}
else
{
// Setting Values For Tabel Columns
$data = array(
//Get data from id dname of textbox to pass into field Student_Name
'Student_Name' => $this->input->post('dname'),
//Get data from id demail of textbox to pass into field Student_Email
'Student_Email' => $this->input->post('demail'),
//Get data from id dmobile of textbox to pass into field Student_Mobile
'Student_Mobile' => $this->input->post('dmobile'),
//Get data from id daddress of textbox to pass into field Student_Address
'Student_Address' => $this->input->post('daddress')
);
// Transfering Data To Model
$this->insert_model->form_insert($data);
// Loading View
$this->load->view('insert_view');
}
}
}
?>

Save this file to "C:\xampp\htdocs\xampp\CodeIgniter\application\controllers"

7- Create file insert_model.php for MODEL

<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Inserting in Table(students) of Database(college)
$this->db->insert('students', $data);
}
}
?>

Save this file to "C:\xampp\htdocs\xampp\CodeIgniter\application\models"

Demo for XAMPP
http://localhost/xampp/CodeIgniter/index.php/insert_ctrl

Read more blog:
http://video-tutorial-from-youtube.blogspot.com/

Follow Us with Facebook
https://www.facebook.com/pages/PHP-Tutorial/782239958529506

Share this

Related Posts

Previous
Next Post »