<?php
/**
* MINDA E-Certificate System - MySQL REST API Endpoint
* Save as: api.php
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Headers: Content-Type');
// Database Configuration
$host = "localhost";
$db_name = "minda_ecert";
$username = "root";
$password = "";
try {
$pdo = new PDO("mysql:host=$host;dbname=$db_name;charset=utf8", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo json_encode(["status" => "error", "message" => "Database Connection Failed: " . $e->getMessage()]);
exit();
}
$action = $_GET['action'] ?? '';
// Handle Actions
switch ($action) {
case 'get_events':
$stmt = $pdo->query("SELECT * FROM events ORDER BY created_at DESC");
echo json_encode(["status" => "success", "data" => $stmt->fetchAll(PDO::FETCH_ASSOC)]);
break;
case 'add_event':
$data = json_decode(file_get_contents('php://input'), true);
$sql = "INSERT INTO events (title, date_time, venue, map_url, status, tentative_img, cert_template_img)
VALUES (:title, :date_time, :venue, :map_url, :status, :tentative_img, :cert_template_img)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':title' => $data['title'],
':date_time' => $data['date_time'],
':venue' => $data['venue'],
':map_url' => $data['map_url'],
':status' => $data['status'],
':tentative_img' => $data['tentative_img'],
':cert_template_img' => $data['cert_template_img']
]);
echo json_encode(["status" => "success", "id" => $pdo->lastInsertId()]);
break;
case 'register_participant':
$data = json_decode(file_get_contents('php://input'), true);
$cert_code = "MINDA-" . strtoupper(substr(md5(uniqid()), 0, 8));
$sql = "INSERT INTO participants (event_id, full_name, ic_no, email, phone, role, cert_code)
VALUES (:event_id, :full_name, :ic_no, :email, :phone, :role, :cert_code)";
$stmt = $pdo->prepare($sql);
$stmt->execute([
':event_id' => $data['event_id'],
':full_name' => $data['full_name'],
':ic_no' => $data['ic_no'],
':email' => $data['email'],
':phone' => $data['phone'],
':role' => $data['role'],
':cert_code' => $cert_code
]);
echo json_encode(["status" => "success", "cert_code" => $cert_code]);
break;
default:
echo json_encode(["status" => "error", "message" => "Invalid API Endpoint"]);
break;
}
?>