// create.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/database.php';
include_once '../class/games.php';
include_once '../class/count.php';
$database = new Database();
$db = $database->getConnection();
// count of records in DB, maximum: 12
$countOfRows = new Count( $db );
$quantity = $countOfRows->getCount();
$game = new Game( $db );
$data = json_decode( file_get_contents( "php://input" ));
// Limitation of records in DB
if ( $quantity < 12 ) {
//game values
if ( !empty($_POST) ) { // request FORM submit
$game->name = $_POST['name'];
$game->game_producer = $_POST['game_producer'];
$game->category = $_POST['category'];
} else { // request JSON
$game->name = $data->name;
$game->game_producer = $data->game_producer;
$game->category = $data->category;
}
if( $game->createGame() ) {
http_response_code( 200 );
echo json_encode('New game was created.');
} else {
echo json_encode('New game could not be created.');
}
} else {
echo json_encode('Limit of records is over. Maximum quantity is 12.');
}
?>
// read.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include_once '../config/database.php';
include_once '../class/games.php';
$database = new Database();
$db = $database->getConnection();
$games = new Game( $db );
$stmt = $games->getGames();
$itemCount = $stmt->rowCount();
if ( $itemCount > 0 ){
$gameArr = array();
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
extract( $row );
$e = array(
"id" => $id,
"name" => $name,
"game_producer" => $game_producer,
"category" => $category
);
array_push( $gameArr, $e );
}
http_response_code( 200 );
echo json_encode( $gameArr );
} else {
http_response_code( 404 );
echo json_encode(
array("message" => "No game found.")
);
}
?>
// single_read.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/database.php';
include_once '../class/games.php';
$database = new Database();
$db = $database->getConnection();
$game = new Game( $db );
$game->id = isset( $_GET['id'] ) ? $_GET['id'] : die();
$game->getSingleGame();
if ( $game->name != null ) {
// create array
$emp_arr = array(
"id" => $game->id,
"name" => $game->name,
"game_producer" => $game->game_producer,
"category" => $game->category
);
http_response_code( 200 );
echo json_encode( $emp_arr );
} else {
http_response_code( 404 );
echo json_encode("Game not found.");
}
?>
// update.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/database.php';
include_once '../class/games.php';
$database = new Database();
$db = $database->getConnection();
$game = new Game( $db );
/**
* Takes raw data from the request
* $json = file_get_contents('php://input');
* Converts it into a PHP object
* $data = json_decode($json);
*/
$data = json_decode( file_get_contents( "php://input" ) );
//game values
if ( !empty( $_POST ) ) { // request FORM submit
$game->id = $_POST['id'];
$game->name = $_POST['name'];
$game->game_producer = $_POST['game_producer'];
$game->category = $_POST['category'];
} else { // request JSON
$game->id = $data->id;
$game->name = $data->name;
$game->game_producer = $data->game_producer;
$game->category = $data->category;
}
if( $game->updateGame() ) {
echo json_encode( "Game was updated " );
} else{
echo json_encode( "Game could not be updated" );
}
?>
// delete.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/database.php';
include_once '../class/games.php';
$database = new Database();
$db = $database->getConnection();
$game = new Game( $db );
$data = json_decode( file_get_contents( "php://input" ) );
//game values
if ( !empty( $_POST ) ) { // request FORM submit
$game->id = $_POST['id'];
} else { // request JSON
$game->id = $data->id;
}
if( $game->deleteGame() ) {
echo json_encode( "Game id: " . $game->id . " was deleted." );
} else {
echo json_encode( "Game id: " . $game->id . " could not be deleted" );
}
?>
// search.php
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include_once '../config/database.php';
include_once '../class/games.php';
$search = $_GET['search'];
$database = new Database();
$db = $database->getConnection();
$games = new Game( $db );
$data = json_decode(file_get_contents( "php://input" ));
if ( empty( $_GET ) ) {
// rewrite from json data
$search = $data->search;
}
if ( !empty( $search ) ) {
$stmt = $games->searchGames( $search );
}
if ( $stmt ) {
echo json_encode( $stmt );
} else {
echo json_encode( 'Not found' );
}
?>
// games.php
<?php
class Game {
// Connection
private $conn;
private $db_table = "videogames";
public $id;
public $name;
public $game_producer;
public $category;
// Db connection
public function __construct( $db ){
$this->conn = $db;
}
// SEARCH
public function searchGames( $d ) {
$d = htmlspecialchars( strip_tags( $d ) );
$search = "%$d%";
$sqlQuery = "
SELECT * FROM
". $this->db_table ."
WHERE `category` LIKE ?";
$stmt = $this->conn->prepare($sqlQuery);
$stmt->execute( [$search] );
$data = $stmt->fetchAll( PDO::FETCH_ASSOC );
return $data;
}
// GET ALL
public function getGames() {
$sqlQuery = "
SELECT
`id`,
`name`,
`game_producer`,
`category`
FROM
" . $this->db_table . "";
$stmt = $this->conn->prepare( $sqlQuery );
$stmt->execute();
return $stmt;
}
// CREATE
public function createGame() {
$sqlQuery = "
INSERT INTO
". $this->db_table ."
SET
name = :name,
game_producer = :game_producer,
category = :category";
$stmt = $this->conn->prepare( $sqlQuery );
// sanitize
$this->name = htmlspecialchars(strip_tags( $this->name ));
$this->game_producer = htmlspecialchars(strip_tags( $this->game_producer ));
$this->category = htmlspecialchars(strip_tags( $this->category ));
// bind data
$stmt->bindParam( ":name", $this->name );
$stmt->bindParam( ":game_producer", $this->game_producer );
$stmt->bindParam( ":category", $this->category );
if( $stmt->execute() ){
return true;
}
return false;
}
// READ single
public function getSingleGame() {
$sqlQuery = "
SELECT
id,
name,
game_producer,
category
FROM
". $this->db_table ."
WHERE
id = ?
LIMIT 0,1";
$stmt = $this->conn->prepare( $sqlQuery );
$stmt->bindParam( 1, $this->id );
$stmt->execute();
$dataRow = $stmt->fetch( PDO::FETCH_ASSOC );
$this->name = $dataRow['name'];
$this->game_producer = $dataRow['game_producer'];
$this->category = $dataRow['category'];
}
// UPDATE
public function updateGame() {
$sqlQuery = "
UPDATE
". $this->db_table ."
SET
name = :name,
game_producer = :game_producer,
category = :category
WHERE
id = :id";
$stmt = $this->conn->prepare( $sqlQuery );
// sanitize
$this->name = htmlspecialchars(strip_tags( $this->name ));
$this->game_producer = htmlspecialchars(strip_tags( $this->game_producer ));
$this->category = htmlspecialchars(strip_tags( $this->category ));
$this->id = htmlspecialchars(strip_tags( $this->id ));
// bind data
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":game_producer", $this->game_producer);
$stmt->bindParam(":category", $this->category);
$stmt->bindParam(":id", $this->id);
if( $stmt->execute() ) {
return true;
}
return false;
}
// DELETE
function deleteGame() {
$sqlQuery = "
DELETE FROM
" . $this->db_table . "
WHERE
id = ?";
$stmt = $this->conn->prepare( $sqlQuery );
$this->id = intval( $this->id ) ;
$stmt->bindParam(1, $this->id);
if( $stmt->execute() ){
return true;
}
return false;
}
}
?>
// database.php
<?php
class Database {
private $host = "host";
private $database_name = "database_name";
private $username = "login";
private $password = "password";
public $conn;
public function getConnection(){
$this->conn = null;
try {
$this->conn = new PDO( "mysql:host=" . $this->host . ";
dbname=" . $this->database_name,
$this->username,
$this->password );
$this->conn->exec( "set names utf8" );
} catch( PDOException $exception ) {
echo "Database could not be connected: " . $exception->getMessage();
}
return $this->conn;
}
}
?>