








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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)
1 / 14
This page cannot be seen from the preview
Don't miss anything!









MVC Model & PHP MVC Project Structure
Section 1
php class config
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
Dashboard
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