62 lines
2.1 KiB
PHP
62 lines
2.1 KiB
PHP
<?php
|
|
require_once 'config.php';
|
|
|
|
try {
|
|
$pdo = db();
|
|
|
|
// Create surveys table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS surveys (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
title VARCHAR(255) NOT NULL,
|
|
description TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Create questions table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS questions (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
survey_id INT NOT NULL,
|
|
question_text VARCHAR(255) NOT NULL,
|
|
question_type VARCHAR(50) NOT NULL,
|
|
options TEXT, -- JSON encoded options for multiple_choice/checkboxes
|
|
FOREIGN KEY (survey_id) REFERENCES surveys(id) ON DELETE CASCADE
|
|
)");
|
|
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS survey_responses (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
survey_id INT NOT NULL,
|
|
submitted_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (survey_id) REFERENCES surveys(id) ON DELETE CASCADE
|
|
)");
|
|
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS survey_answers (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
response_id INT NOT NULL,
|
|
question_id INT NOT NULL,
|
|
answer_text TEXT,
|
|
FOREIGN KEY (response_id) REFERENCES survey_responses(id) ON DELETE CASCADE,
|
|
FOREIGN KEY (question_id) REFERENCES questions(id) ON DELETE CASCADE
|
|
)");
|
|
|
|
// Create users table
|
|
$pdo->exec("CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50) NOT NULL UNIQUE,
|
|
password VARCHAR(255) NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)");
|
|
|
|
// Insert a default admin user if one doesn't exist
|
|
$stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
|
|
$stmt->execute(['admin']);
|
|
if ($stmt->rowCount() == 0) {
|
|
$username = 'admin';
|
|
$password = password_hash('password', PASSWORD_DEFAULT);
|
|
$pdo->prepare("INSERT INTO users (username, password) VALUES (?, ?)")->execute([$username, $password]);
|
|
}
|
|
|
|
echo "Database tables created successfully!";
|
|
|
|
} catch (PDOException $e) {
|
|
die("Database migration failed: " . $e->getMessage());
|
|
} |