Translate

Saturday, 19 September 2020

Codeigniter 4 Insert Data in Database


1) Without Model (using Query Builder) insert data in database in codeigniter 4

Controller -  User.php
location - App/Controller/User.php


     <?php namespace App\Controllers;
     use App\Models\UserModel;
     use CodeIgniter\Controller;

     class User extends BaseController
     {
             public function index()
         {
             return view('home');
         }

         public function insert()
         {
             $data = [ 'name' =>$this->request->getVar('name'),
                 'email' =>$this->request->getVar('email'),
                 'password' =>$this->request->getVar('password') ];

             $db = \Config\Database::connect();
             $builder = $db->table('fullname');
             $builder->insert($data);
         }
     }
     ?>

View - home.php

Location - app/Views/home.php


     <!DOCTYPE html>
     <html lang="en">
     <head>
     <title>Home Page</title>
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
     </head>
     <body>
     <div class="container">
     <h1>Insert Data</h1>
     <form action="<?php echo site_url('User/insert');?>" method="post">
     <div class="form-group">
         <label for="name">Name:</label>
         <input type="text" class="form-control" id="name" placeholder="Enter Name" name="name">
     </div>
     <div class="form-group">
         <label for="email">Email:</label>
         <input type="email" class="form-control" id="email" placeholder="Enter email" name="email">
     </div>
     <div class="form-group">
         <label for="pwd">Password:</label>
         <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="password">
     </div>
     <button type="submit" class="btn btn-primary">Submit</button>
     </form>
     </div>
     </body>
     </html>
    



2) Codeigniter 4 Insert Data in Database Using Model

  • View File Home.php is same you have to change in controller and add UserModel.php file.
Model - UserModel.php
Location - app/Models/UserModel.php

<?php namespace App\Models;
use CodeIgniter\Model;

class UserModel extends Model
{
     protected $table = 'fullname'; //table name
     protected $allowedFields= ['name','surname','email','password']; //column name of table
}
?>

Controller -  User.php
location - app/Controller/User.php

<?php namespace App\Controllers;
use App\Models\UserModel;
use CodeIgniter\Controller;

class User extends BaseController
{
     public function index()
     {
         return view('home');
     }

     public function insert()
     {
         $data = [ 'name' =>$this->request->getVar('name'),
             'email' =>$this->request->getVar('email'),
             'password' =>$this->request->getVar('password') ];

         $model = new UserModel();
         $model->insert($data);
     }
}
?>



This You Tube Video has full explaination with Practical, you can watch that video.



Thanks.