Here we are assuming that there is a controller method to submit a registration form.
we had created a Database Connection and a table of ‘users’.
STEP 1:
Create app/Models/HomeModel.php
STEP 2:
Load \Config\Services::request(); in __construct for getting input value.
1 2 3 4 5 6 | protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; $this->session = \Config\Services::session(); $this->request = \Config\Services::request(); } |
Complete Code of Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php namespace App\Models; use CodeIgniter\Model; use CodeIgniter\Database\ConnectionInterface; class HomeModel extends Model { protected $db; public function __construct(ConnectionInterface &$db) { $this->db =& $db; $this->session = \Config\Services::session(); $this->request = \Config\Services::request(); } public function register(){ $name = $this->request->getPost('name'); $phone = $this->request->getPost('phone'); $email = $this->request->getPost('email'); $organization = $this->request->getPost('organization'); $designation = $this->request->getPost('designation'); $useCase = $this->request->getPost('useCase'); $hear_from = $this->request->getPost('hear_from'); $req = $this->request->getPost('req'); $data = [ 'name' => $name, 'phone' => $phone, 'email' => $email, 'organization' => $organization, 'designation' => $designation, 'useCase' => $useCase, 'hear_from' => $hear_from, 'req' => $req, 'status' => '1' ]; $result = $this->db ->table('users') ->insert($data); $affected_rows = $this->db->affectedRows(); if($affected_rows > 0){ return 1; }else{ return 0; } } } ?> |