Course Content
Introduction to CodeIgniter 4
CodeIgniter is an Application Development Framework. CodeIgniter is a popular and powerful MVC (Model-View-Controller) framework that is used to develop web applications. It is a free and Open-source PHP framework.
0/5
MVC (Model-View-Controller)
MVC stands for Model-View-Controller. MVC is an application design model consisting of three interconnected parts. They include the model (data), the view (user interface), and the controller (processes that handle input).
0/6
Sessions
The Session class allows you to maintain a user’s "state" and track their activity while they browse your site.
0/1
URI Routing
There is a one-to-one relationship between a URL string and its corresponding controller class/method.
0/2
Working with Database
Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides a rich set of functionalities to interact with the database.
0/5
Spreadsheet
PhpSpreadsheet is a PHP library for reading and writing spreadsheet files. Importing Excel and CSV into MySQL help to save the user time and avoid repetitive work.
0/1
CodeIgniter 4
About Lesson

The insert() method accepts the associative array of data and is passed into this method as the only parameter to create a new row of data in the database.

The array’s keys must match the name of the columns in a $table, while the array’s values are the values to save for that key:

Syntax:

$this->db->table('table_name')->insert('array_of_objects');

Example:

$data = [
    'username' => 'darth',
    'email'    => 'd.vader@theempire.com',
];
$this->db->table($table)->insert($data);

Create a new row of data in the database

1. Create a user_info table in the MySQL database.

CREATE TABLE `user_info` ( 
`id` INT NOT NULL AUTO_INCREMENT , 
`first_name` VARCHAR(255) NOT NULL , 
`last_name` VARCHAR(255) NOT NULL , 
`email` VARCHAR(255) NOT NULL , 
PRIMARY KEY (`id`)
) ENGINE = InnoDB;

2. Copy and Paste the following lines in app/Config/Routes.php

$routes->add('user/add', 'User::add');
$routes->post('user/save', 'User::save');

3. Create an add.php file in app/Views/ directory

<!DOCTYPE html>
<html>

<head>
    <title>Add user</title>
</head>

<body>
    <form method="post" action="<?php echo base_url('user/save'); ?>">
        <table width="600" border="1" cellspacing="5" cellpadding="5">
            <tr>
                <td width="230">First Name</td>
                <td width="329">
                    <input type="text" name="txtFirstName" />
                </td>
            </tr>
            <tr>
                <td>Last Name</td>
                <td>
                    <input type="text" name="txtLastName" />
                </td>
            </tr>
            <tr>
                <td>Email ID</td>
                <td>
                    <input type="email" name="txtEmail" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="btnAddUser" value="Add user" />
                </td>
            </tr>
        </table>
    </form>
</body>

</html>

4. Create a model file UserModel.php in the app/Models/ directory.

<?php 
namespace AppModels;

use CodeIgniterModel;
use CodeIgniterDatabase\ConnectionInterface;

class UserModel extends Model {
    
    protected $db;
    public function __construct(ConnectionInterface &$db) {
        $this->db =& $db;
    }

    function add($data) {
        return $this->db
                        ->table('user_info')
                        ->insert($data);
    }
    
}

5. Create a controller file User.php in the app/Controllers/ directory.

<?php
namespace AppControllers;

use AppModelsUserModel;

class User extends BaseController {

    public function __construct() {

        $db = db_connect();
        $this->userModel = new UserModel($db);
    }

    public function add() {
        echo view('add');
    }

    public function save() {
        $first_name = $this->request->getPost('txtLastName');
        $last_name  = $this->request->getPost('txtLastName');
        $email      = $this->request->getPost('txtEmail');
    
        $data = [
            'first_name'        => $first_name,
            'last_name'     => $last_name,
            'email'         => $email,
        ];

        $result = $this->userModel->add($data);
        if($result) {
            echo "New user is registered successfully.";
        } else {
            echo "Something went wrong";
        }
    }
}