In this tutorial, we’ll understand way how to insert data into a database using the model and view and Controller.
1-Table structure for table tblusers
CREATE TABLE `tblusers` ( `id` int(11) NOT NULL, `name` varchar(45) DEFAULT NULL, `email` varchar(45) DEFAULT NULL, `phone` varchar(45) DEFAULT NULL, `gender` varchar(45) DEFAULT NULL, `create_date` timestamp NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2-Database configuration
insertci/application/config/database.php
$active_group = 'default'; $query_builder = TRUE; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', //localhost 'username' => 'root', //username 'password' => '', //password 'database' => 'student_info', //databasename 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
3-Add to Libraries and helper
insertci/application/config/routes.php
$autoload['libraries'] = array('database','session','form_validation'); $autoload['helper'] = array('html','url','form');
4-Create a view data(userform.php)
insertci/application/views/userform.php
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <div class="container"> <div class="row centered-form"> <div class="col-sm-4 "> <div class="panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Add New Record </h3> <p style="color:green"><?php echo $this->session->flashdata('success');?></p> <p style="color:red"><?php echo $this->session->flashdata('error');?></p> <?php echo form_error('name','<div style="color:red">','<div>');?> <?php echo form_error('email','<div style="color:red">','<div>');?> <?php echo form_error('phone','<div style="color:red">','<div>');?> <?php echo form_error('gender','<div style="color:red">','<div>');?> </div> <div class="panel-body"> <?php echo form_open('Insertdata',['name'=>'insert']);?> <div class="form-group"> <label for="name">Name</label> <?php echo form_input(['name'=>'name','id'=>'name','class'=>'form-control input-sm','placeholder'=>'Enter name','value'=>set_value('name')]);?> </div> <div class="form-group"> <label for="email">Email</label> <?php echo form_input(['name'=>'email','id'=>'email','class'=>'form-control input-sm','placeholder'=>'Email Address','value'=>set_value('email')]);?> </div> <div class="form-group"> <label for="phone">Phone</label> <?php echo form_input(['name'=>'phone','id'=>'phone','class'=>'form-control input-sm','placeholder'=>'Enter Phone','value'=>set_value('phone')]);?> </div> <p> <div class="form-group"> <label for="phone">Gender</label> <?php $options=array( 'Male'=>'Male', 'Female'=>'Female', 'Other'=>'Other' ); echo form_dropdown('gender',$options,set_value('gender'),'class="form-control"');?> </div> <div class="col-sm-6 "> <?php echo form_submit(['name'=>'submit','id'=>'submit','class'=>'btn btn-info btn-block','value'=>'Submit']);?></center> </div> <?php echo form_close();?> </div> </div> </div> </div> </div>
5-By default controller set(Insertdata)
insertci/application/config/routes.php
$route['default_controller'] = 'Insertdata';
6-Create a Controller(Insertdata.php)
insertci/application/controllers/Insertdata.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Insertdata extends CI_Controller { public function index() { $this->form_validation->set_rules('name','Name','required'); $this->form_validation->set_rules('email','Email','required'); $this->form_validation->set_rules('phone','Phpne','required'); $this->form_validation->set_rules('gender','Gender','required'); if($this->form_validation->run()) { $name=$this->input->post('name'); $email=$this->input->post('email'); $phone=$this->input->post('phone'); $gender=$this->input->post('gender'); $this->load->model('Addrecord'); $this->Addrecord->add($name,$email,$phone,$gender); } else{ $this->load->view('userform'); } } }
7-Create a Model(Showrecord.php)
insertci/application/models/Addrecord.php
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Addrecord extends CI_Model{ //for getting user details public function add($name,$email,$phone,$gender){ $data=array( 'name'=>$name, 'email'=>$email, 'phone'=>$phone, 'gender'=>$gender ); $query=$this->db->insert('tblusers',$data); if($query){ $this->session->set_flashdata('success','Add New Record successfully.'); redirect('Insertdata'); } else { $this->session->set_flashdata('error','Record not successfully.'); redirect('Insertdata'); } } }
How To Insert data Using PHP Codeigniter With Source code
Size: 2MB
Version: 1.0
326 Downloads
Leave a Comment