PHP Controller Code for Sports Database Management, Essays (high school) of History

PHP code for a controller that manages CRUD operations for a sports database. The controller interacts with the model and view layers to perform actions such as adding, updating, deleting, and listing sports records in the database.

Typology: Essays (high school)

2010/2011

Uploaded on 10/03/2022

Papayayax
Papayayax ๐Ÿ‡ป๐Ÿ‡ณ

4.8

(13)

144 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MySQL PHP MVC CRUD Without
Framework
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,!it 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.
PROJECT WEB - WEBG301 1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download PHP Controller Code for Sports Database Management and more Essays (high school) History in PDF only on Docsity!

MySQL PHP MVC CRUD Without

Framework

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, it 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

In PhpMyAdmin, create a database named php_mvc_demo then create a table named sports

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

to access the function sportsController. The sportsModel class constructor receives a mysql

connection parameter to work with the database.

sportsModel.php

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 ("Error in connection: ". $this->condb->connect_error); } } // close database public function close_db() { $this->condb->close(); } // insert record public function insertRecord($obj) { try { $this->open_db(); $query=$this->condb->prepare("INSERT INTO sports (category,name) VALUES (?, ?)"); $query->bind_param("ss",$obj->category,$obj->name); $query->execute(); $res= $query->get_result(); $last_id=$this->condb->insert_id; $query->close(); $this->close_db(); return $last_id; } catch (Exception $e) { $this->close_db(); throw $e; } } //update record

public function updateRecord($obj) { try { $this->open_db(); $query=$this->condb->prepare("UPDATE sports SET category=?,name=? WHERE id=?"); $query->bind_param("ssi", $obj->category,$obj->name,$obj->id); $query->execute(); $res=$query->get_result(); $query->close(); $this->close_db(); return true ; } catch (Exception $e) { $this->close_db(); throw $e; } } // delete record public function deleteRecord($id) { try { $this->open_db(); $query=$this->condb->prepare("DELETE FROM sports WHERE id=?"); $query->bind_param("i",$id); $query->execute(); $res=$query->get_result(); $query->close(); $this->close_db(); return true ; } catch (Exception $e) { $this->closeDb(); throw $e; } } // select record public function selectRecord($id) { try { $this->open_db(); if ($id> 0 ) { $query=$this->condb->prepare("SELECT * FROM sports WHERE id=?"); $query->bind_param("i",$id); } else {$query=$this->condb->prepare("SELECT * FROM sports"); } $query->execute(); $res=$query->get_result();

default : $this-> list (); } } // page redirection public function pageRedirect($url) { header ('Location:'.$url); } // check validation public function checkValidation($sporttb) { $noerror= true ; // Validate category if ( empty ($sporttb->category)){ $sporttb->category_msg = "Field is empty."; $noerror= false ; } elseif (! filter_var ($sporttb->category, FILTER_VALIDATE_REGEXP , array ("options"=> array ("regexp"=>"/^[a-zA-Z\s]+$/")))){ $sporttb->category_msg = "Invalid entry."; $noerror= false ; } else {$sporttb->category_msg ="";} // Validate name if ( empty ($sporttb->name)){ $sporttb->name_msg = "Field is empty."; $noerror= false ; } elseif (! filter_var ($sporttb->name, FILTER_VALIDATE_REGEXP , array ("options"=> array ("regexp"=>"/^[a-zA-Z\s]+$/")))){ $sporttb->name_msg = "Invalid entry."; $noerror= false ; } else {$sporttb->name_msg ="";} return $noerror; } // add new record public function insert() { try { $sporttb= new sports(); if ( isset ($_POST['addbtn'])) { // read form value $sporttb->category = trim ($_POST['category']); $sporttb->name = trim ($_POST['name']); //call validation $chk=$this->checkValidation($sporttb); if ($chk) { //call insert record $pid = $this -> objsm ->insertRecord($sporttb); if ($pid> 0 ){ $this-> list (); } else { echo "Somthing is wrong..., try again."; } } else { $_SESSION['sporttbl0']= serialize ($sporttb);//add

session obj $this->pageRedirect("view/insert.php"); } } } catch (Exception $e) { $this->close_db(); throw $e; } } // update record public function update() { try { if ( isset ($_POST['updatebtn'])) { $sporttb= unserialize ($_SESSION['sporttbl0']); $sporttb->id = trim ($_POST['id']); $sporttb->category = trim ($_POST['category']); $sporttb->name = trim ($_POST['name']); // check validation $chk=$this->checkValidation($sporttb); if ($chk) { $res = $this -> objsm ->updateRecord($sporttb); if ($res){ $this-> list (); } else { echo "Somthing is wrong..., try again."; } } else { $_SESSION['sporttbl0']= serialize ($sporttb); $this->pageRedirect("view/update.php"); } } elseif ( isset ($_GET["id"]) &&! empty ( trim ($_GET["id"]))){ $id=$_GET['id']; $result=$this->objsm->selectRecord($id); $row= mysqli_fetch_array ($result); $sporttb= new sports(); $sporttb->id=$row["id"]; $sporttb->name=$row["name"]; $sporttb->category=$row["category"]; $_SESSION['sporttbl0']= serialize ($sporttb); $this->pageRedirect('view/update.php'); } else { echo "Invalid operation."; } } catch (Exception $e) { $this->close_db(); throw $e; } }

Add Sports

**Please fill this form and submit to add sports record in the database.**

category_msg))? 'has-error' : ''; ?> " > Sport Category category; ?> " > category_msg;?>

name_msg))? 'has-error' : ''; ?> " > Sports Name name; ?> " > name_msg;?>

Cancel

list.php

Dashboard

update.php

Create Record

Update Sports

**Please fill this form and submit to add sports record in the database.**

category_msg))? 'has-error' : ''; ?> " > Sport Category category; ?> " > php echo $sporttb-

>category_msg;?>

name_msg))? 'has-error' : ''; ?> " > Sports Name name; ?> " > name_msg;?>

id; ?> " />

Cancel