39199-vm/includes/registration_db.php
Flatlogic Bot 270903d716 roni
2026-03-15 02:57:10 +00:00

51 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/../db/config.php';
function ensure_registration_table(): void {
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS registrations (
id INT AUTO_INCREMENT PRIMARY KEY,
reg_code VARCHAR(32) NOT NULL UNIQUE,
name VARCHAR(190) NOT NULL,
education VARCHAR(20) NOT NULL,
major VARCHAR(190) NOT NULL,
photo_path VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
SQL;
db()->exec($sql);
}
function generate_reg_code(): string {
return 'REG-' . date('Ymd') . '-' . strtoupper(bin2hex(random_bytes(3)));
}
function insert_registration(array $data): int {
$stmt = db()->prepare(
"INSERT INTO registrations (reg_code, name, education, major, photo_path)
VALUES (:reg_code, :name, :education, :major, :photo_path)"
);
$stmt->bindValue(':reg_code', $data['reg_code']);
$stmt->bindValue(':name', $data['name']);
$stmt->bindValue(':education', $data['education']);
$stmt->bindValue(':major', $data['major']);
$stmt->bindValue(':photo_path', $data['photo_path']);
$stmt->execute();
return (int)db()->lastInsertId();
}
function fetch_registrations(): array {
$stmt = db()->query("SELECT * FROM registrations ORDER BY created_at DESC");
return $stmt->fetchAll();
}
function fetch_registration(int $id): ?array {
$stmt = db()->prepare("SELECT * FROM registrations WHERE id = :id");
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetch();
return $row ?: null;
}