52 lines
2.0 KiB
PHP
52 lines
2.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// SQL statements for creating tables
|
|
$sql = <<<SQL
|
|
CREATE TABLE IF NOT EXISTS `participants` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`email` VARCHAR(255) NOT NULL UNIQUE,
|
|
`nickname` VARCHAR(255) NOT NULL,
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
CREATE TABLE IF NOT EXISTS `questions` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`question_text` TEXT NOT NULL,
|
|
`score` INT NOT NULL DEFAULT 10,
|
|
`image_url` VARCHAR(255) NULL,
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
CREATE TABLE IF NOT EXISTS `answers` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`question_id` INT NOT NULL,
|
|
`answer_text` VARCHAR(255) NOT NULL,
|
|
`is_correct` BOOLEAN NOT NULL DEFAULT FALSE,
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
CREATE TABLE IF NOT EXISTS `submissions` (
|
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
|
`participant_id` INT NOT NULL,
|
|
`question_id` INT NOT NULL,
|
|
`answer_id` INT NOT NULL,
|
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (`participant_id`) REFERENCES `participants`(`id`) ON DELETE CASCADE,
|
|
FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON DELETE CASCADE,
|
|
FOREIGN KEY (`answer_id`) REFERENCES `answers`(`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
SQL;
|
|
|
|
$pdo->exec($sql);
|
|
echo "Tabelle create con successo (o già esistenti)." . PHP_EOL;
|
|
|
|
} catch (PDOException $e) {
|
|
die("Errore nella creazione delle tabelle: " . $e->getMessage());
|
|
}
|