Introduction
This tutorial is for beginners or students. I created a functionality to add, edit and delete a record in PHP with MVC logic without Framework. Also, explained how to create an MVC pattern in PHP. I hope it will be helpful for you to add a data table in your program.
Building Our MVC Framework Pattern in PHP
You might be wondering why we would even need to create our own framework when there are already so many good choices out there. The reason for this is so that we can gain an understanding of the underlying principles of MVC.
As we learn more about these principles, we will grow in our understanding of why the excellent MVC frameworks do things the way they do. We are not learning how to create an application in Zend Framework, or in CakePHP. We are learning how MVC works, and by extension, how these frameworks have built upon (or deviated from) the way in which we would expect an MVC framework to be built.
MVC Model & PHP MVC Project Structure

Section 1
Config.php is used to connect the mysql database to create a connection parameter for mysql host,user,password and database name.
<?php
class config
{
function __construct() {
$this->host = "localhost";
$this->user = "root";
$this->pass = "welcome";
$this->db = "mydb13";
}
}
?>
A small part of the code in index.php is used to setup a controller object and call mvcHandler() to view the default page list.php.
<?php
session_unset();
require_once 'controller/sportsController.php';
$controller = new sportsController();
$controller->mvcHandler();
?>
In the model folder, create one class for table structure, its named sports and has field and message field to hold messages and data.
<?php
class sports
{
// table fields
public $id;
public $category;
public $name;
// message string
public $id_msg;
public $category_msg;
public $name_msg;
// constructor set default value
function __construct()
{
$id=0;$category=$name="";
$id_msg=$category_msg=$name_msg="";
}
}
?>
Section 2
The second section is a sportsModel class structure. We are going to explain and show insertRecord(), updateRecord(),selectRecord() and insertRecord(). The sportsModel class is used to access the function sportsController. The sportsModel class constructor receives a mysql connection parameter to work with the database.
<?php
class sportsModel
{
// set database config for mysql
function __construct($consetup)
{
$this->host = $consetup->host;
$this->user = $consetup->user;
$this->pass = $consetup->pass;
$this->db = $consetup->db;
}
// open mysql data base
public function open_db()
{
$this->condb=new mysqli($this->host,$this->user,$this->pass,$this->db);
if ($this->condb->connect_error)
{
die("Erron in connection: " . $this->condb->connect_error);
}
}
// close database
public function close_db()
{
$this->condb->close();
}
// insert record
public function insertRecord($obj){ }
//update record
public function updateRecord($obj){ }
// delete record
public function deleteRecord($id){ }
// select record
public function selectRecord($id){ }
}
?>
Section 3
Section 3 is the controller code part. The sportsController has mvcHandler() and the CRUD functions insert(), update(),delete() and list(). mvcHandler() receives request and execute. This request shows views according to call request by user.

Join the conversation! Your thoughts help the community grow.