codeigniter insert data into database - Web Development and Web Design Codes

Latest

Monday, February 5, 2018

codeigniter insert data into database

Codeigniter insert data into database

codeigniter insert data into database
codeigniter insert data into database

In this lesson we are going to learn how Insert data into database with Codeigniter. follow the below step to learn how Insert data into database with Codeigniter

=>Database and Table:
Database Name: test
Table Name: user
SQL Codes for table user
CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
//Some Configuration:
Base Url:
go to application/config/config.php and configure look like below:
$config['base_url'] = 'http://localhost/codeigniter/';
Autoload Url:
go to application/config/autoload.php and configure look like below:
$autoload['helper'] = array('url');
Database Configuration:
application/config/database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'test', //Your Database table name here
'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
);

Files:
1. Blog.php (Controller)
go to codeigniter/application/controllers and create Blog.php file.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Blog extends CI_Controller {
   
 public function index()
 {
  $this->load->view('index');
 }
 public function insert_data()
 {
  //get data from index
  $data = array(
    'username'=> $this->input->post('username'),
    'email'=> $this->input->post('email'),
    );
  //call model and insert function 
    
  $this->load->model('User');
  $this->User->insert_user($data);
  
  //send message 
  $data['message'] = 'Data Inserted Successfully';
  $this->load->view('index', $data);
 }
}
2. User.php (Model)
go to codeigniter/application/models and create User.php file.
<?php
class User extends CI_Model
{
 public function insert_user($data)
 {
  //user is table name here
 $this->load->database();
 $this->db->insert('user', $data);
 }
}
 
?>
3. index.php (View):
go to codeigniter/application/views and create index.php file.
<html>
  <head>
   <title>Insert Data Into Database in Codeigniter-- http://codenair.com</title>
  </head>
  <h2>Codeigniter Insert data into database</h2>
  <form method="POST" action="<?php echo base_url();?>index.php/blog/insert_data">
  <table>
 <tr>
  <td>UserName: </td>
  <td><input type="text" name="username" required/> </td>
 </tr>
 <tr>
  <td>Email: </td>
  <td><input type="text" name="email" required/> </td>
 </tr>
  <tr>
  <td></td>
  <td><button type="submit" name="insert">Insert</button> </td>
 </tr>
 </table>
 </form>
</html>
<?php if(isset($message)){echo $message;}?>
Result:
Now open below url from your browser
 http://localhost/codeigniter/index.php/blog
Learn how to remove index.php from codeigniter
codeigniter insert data into database
codeigniter insert data into database

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.