Last active
November 14, 2019 20:00
-
-
Save t-seannn/52d4ed42cb4e340c33ed244ca2ec6234 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Headers | |
header('Access-Control-Allow-Origin: *'); | |
header('Content-Type: application/json'); | |
header('Access-Control-Allow-Methods: POST'); | |
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Content-Type, Access-Control-Allow-Methods, Authorization, X-Requested-With'); | |
include_once '../../config/Database.php'; | |
include_once '../../models/Players.php'; | |
// Instantiate DB & connect | |
$database = new Database(); | |
$db = $database->connect(); | |
// Instantiate tournament object | |
$players = new Players($db); | |
// Get the raw posted data | |
$data = json_decode(file_get_contents("php://input")); | |
$players->playername = $data->playername; | |
$players->clubname = $data->clubname; | |
$players->gender = $data->gender; | |
$players->dob = $data->dob; | |
$players->ageGroupId = $data->ageGroupId; | |
$players->weightCategoryId = $data->weightCategoryId; | |
// Create player | |
if($players->create()){ | |
echo json_encode()( | |
array('message' => 'Player Created') | |
); | |
} else { | |
echo json_encode()( | |
array('message' => 'Player Is Not Created') | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
// DB Parameters | |
private $host = 'localhost'; | |
private $db_name = 'tournamentProject'; | |
private $username = 'root'; | |
private $password = ''; | |
private $conn; | |
// DB Connect | |
public function connect(){ | |
$this->conn = null; | |
try { | |
$this->conn = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->db_name, | |
$this->username, $this->password); | |
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
} catch(PDOException $e){ | |
echo 'Connection Error: ' . $e->getMessage(); | |
} | |
return $this->conn; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Players{ | |
// DB Stuff | |
private $conn; | |
private $table = 'players'; | |
// Properties | |
public $id; | |
public $playername; | |
public $clubname; | |
public $gender; | |
public $dob; | |
public $ageGroupId; | |
public $weightCategoryId; | |
// Constructor with DB | |
public function __construct($db){ | |
$this->conn = $db; | |
} | |
// Get Tournament | |
public function read(){ | |
// Create query | |
echo 'SELECT * from players'; | |
$query = 'SELECT id, playername, clubname, gender, dob, ageGroupId, weightCategoryId | |
FROM | |
' . $this->table . ' | |
'; | |
// Prepare statement | |
$stmt = $this->conn->prepare($query); | |
// Execute query | |
$stmt->execute(); | |
return $stmt; | |
} | |
// Create Players | |
public function create(){ | |
// Create query | |
$query = 'INSERT INTO ' . $this->table . ' | |
SET | |
playername = :playername, | |
clubname = :clubname, | |
gender = :gender, | |
dob = :dob, | |
ageGroupId = :ageGroupId, | |
weightCategoryId = :weightCategoryId'; | |
// Prepare statement | |
$stmt = $this->conn->prepare($query); | |
// Clean data | |
$this->playername = htmlspecialchars(strip_tags($this->playername)); | |
$this->clubname = htmlspecialchars(strip_tags($this->clubname)); | |
$this->gender = htmlspecialchars(strip_tags($this->gender)); | |
$this->dob = htmlspecialchars(strip_tags($this->dob)); | |
$this->ageGroupId = htmlspecialchars(strip_tags($this->ageGroupId)); | |
$this->weightCategoryId = htmlspecialchars(strip_tags($this->weightCategoryId)); | |
// Bind data | |
$stmt->bindParam(':playername', $this->playername); | |
$stmt->bindParam(':clubname', $this->clubname); | |
$stmt->bindParam(':gender', $this->gender); | |
$stmt->bindParam(':dob', $this->dob); | |
$stmt->bindParam(':ageGroupId', $this->ageGroupId); | |
$stmt->bindParam(':weightCategoryId', $this->weightCategoryId); | |
// Execute query | |
if($stmt->execute()){ | |
return true; | |
} | |
// Print error if something goes wrong | |
printf("ERROR: %s.\n", $stmt->error); | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment