Sample PHP code - converted to OOP and MVC

This shows how my procedural PHP sample code can be easily converted 
to OOP and MVC. It is very basic and has no filtering of input. This 
means that input will be sent directly to the database for processing, 
as is, without any checking for incomplete, invalid or malicious input, 
so it is not safe for a production environment.  

The basic architecture, in terms of folders and files, is described first, 
followed by a description of the various files.

The application assumes that the document root on the web server will have 
a folder called 'Autos'. Inside are two more folders: 
'Controllers' and 'Views'.

In the application root directory ('Autos') is a file called 'index.php'.
It only redirects the user to the only Views page, so ONLY contains:

<?php
header('Location: http://localhost/Autos/Views/index.php') ;
?>

That other 'index.php' file, and only 'Views' page, does this:
 - displays a form to insert a new record into a MySQL database, 
     if not already submitted, and submits it to the main controller, 
     which inserts the record (without any filtering!), then retrieves 
     the same record and makes it available to the Views page. 
 - if submitted, simply displays the record, that the controller has 
     made available to it.

It looks like this:



<?php
// This is the content of file 'Autos/Views/index.php':

if (!isset($_POST['submit'])) 
  {
?>

<h1>Please enter the following information for your new car:</h1>
<form action="../Controllers/MainController.php" method="post">
Make: <input type="text" name="Make" size="20"> <br>
Model: <input type="text" name="Model" size="20"> <br>
Year: <input type="text" name="Year" size="20"> <br>
<input type="submit" name="submit" value="Next"> </form>
</form> <!-- End of form -->

<?php
  }

else {

echo '<h1>Thank you</h1>
We have entered the following new record into our database:
<br />';
 {
  while($row = mysql_fetch_array($DB -> result2)) {
    echo "Make: ". $row['Make'].", 
          Model: ".$row['Model'].", and
          Year: ". $row['Year'].".";
    echo "<br />"; }
 }
     }

?>



<?php
// This is the content of file 'Autos/Controllers/MainController.php':

require_once("DBcontroller.php");

$DB = new DBControl();
$DB -> dbconnect();
$DB -> insert();
$DB -> select();

include("../Views/index.php");

$DB -> close();

?>
 


<?php
// This is the content of file 'Autos/Controllers/DBController.php':

class DBControl {

private $host;
private $user;
private $pass;
private $db;
private $conn;

public $result2;

function dbconnect() {
require_once ('..\..\..\xyz\xyz.php');
  } // end of function dbconnect()

function insert() {
   $a=$_REQUEST['Make'];
   $b=$_REQUEST['Model'];
   $c=$_REQUEST['Year'];
   $sql  = "INSERT INTO `cars` (`Make`, `Model`, `Year`) VALUES ";
   $sql .= '(\''   .$a.  '\', \'' .$b. '\', \'' .$c. '\');';
   $result = mysql_query($sql, $this -> conn);
  } // end of function insert() 

function select() { 
   $sql2 = "SELECT `Make`, `Model`, `Year` FROM cars ";
   $sql2 .= "where `id` = LAST_INSERT_ID();";
   $this->result2 = mysql_query($sql2, $this -> conn);
  } // end of function select()

function close() { // close connection
   mysql_close($this -> conn);
  } // end of function close()
   
} // end of class DBControl

?>
 

<?php
// This is the content of file '..xyz/yyz.php' (outside document root):
// Assumes you created BOTH a database 'Yourcars' AND a table 'cars'
// Also assumes you created user, password and privileges needed.  
// For sample MySQL code, see below

$host='yourhost';
$user='youruser';
$pass='yourpassword';
$db='Yourcars';

   $this->host = $host;
   $this->user = $user;
   $this->pass = $pass;
   $this->db = $db;
   $this->conn = mysql_connect($this->host, $this->user, $this->pass, true);
   $db = mysql_select_db($this->db, $this->conn); 
?>
 

You can create the database, table and user needed as follows, 
from the MySQL console:

use mysql;
-- show databases;
DROP DATABASE IF EXISTS Yourcars;
CREATE DATABASE Yourcars;

use Yourcars;
CREATE TABLE `cars` 
(
`id` int NOT NULL AUTO_INCREMENT
, PRIMARY KEY  (`id`)
, UNIQUE KEY `id` (`id`)
, `Make`  varchar(20) default NULL
, `Model` varchar(20) default NULL
, `Year`  int default NULL
, `Date & Time Entered` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, 
ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON cars.* TO 
'youruser'@'yourhost' IDENTIFIED BY 'yourpassword';



Valid XHTML 1.0 Transitional Valid CSS!
 

MCP icon
MCTS icon