archinsa/init_db.php

75 lines
1.9 KiB
PHP
Raw Normal View History

2023-10-22 19:24:59 +02:00
<?php
// Database connection parameters
2023-11-03 22:22:27 +01:00
include("test_creds.php");
2023-10-22 19:24:59 +02:00
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create tables
$sql = "
CREATE TABLE IF NOT EXISTS themes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
2023-11-05 16:49:48 +01:00
CREATE TABLE IF NOT EXISTS ensembles (
2023-10-22 19:24:59 +02:00
id INT AUTO_INCREMENT PRIMARY KEY,
2023-11-03 22:22:27 +01:00
commentaire_auteur TEXT,
2023-12-23 21:42:28 +01:00
valide BOOLEAN NOT NULL DEFAULT FALSE,
corrige_inclu BOOLEAN NOT NULL DEFAULT FALSE,
date_televersement DATE NOT NULL DEFAULT CURRENT_DATE()
2023-10-22 19:24:59 +02:00
);
CREATE TABLE IF NOT EXISTS documents (
id INT AUTO_INCREMENT PRIMARY KEY,
titre VARCHAR(255) NOT NULL,
type INT,
upload_path TEXT NOT NULL,
commentaire_auteur TEXT,
ensemble_id INT,
theme_id INT,
FOREIGN KEY (theme_id) REFERENCES themes(id),
2023-11-05 17:03:58 +01:00
FOREIGN KEY (ensemble_id) REFERENCES ensembles(id)
2023-10-22 19:24:59 +02:00
);
CREATE TABLE IF NOT EXISTS exercices (
id INT AUTO_INCREMENT PRIMARY KEY,
commentaire_auteur TEXT,
2023-11-05 16:49:48 +01:00
ensemble_id INT,
2023-12-23 21:42:28 +01:00
document_id INT,
2023-11-05 17:03:58 +01:00
duree INT,
2023-12-23 21:42:28 +01:00
FOREIGN KEY (ensemble_id) REFERENCES ensembles(id),
FOREIGN KEY (document_id) REFERENCES documents(id)
2023-10-22 19:24:59 +02:00
);
CREATE TABLE IF NOT EXISTS exercices_themes (
id INT AUTO_INCREMENT PRIMARY KEY,
exercice_id INT,
ensemble_id INT,
2023-10-22 19:24:59 +02:00
theme_id INT,
FOREIGN KEY (exercice_id) REFERENCES exercices(id),
FOREIGN KEY (ensemble_id) REFERENCES ensembles(id),
2023-10-22 19:24:59 +02:00
FOREIGN KEY (theme_id) REFERENCES themes(id)
);
";
if ($conn->multi_query($sql) === TRUE) {
echo "Tables created successfully";
} else {
echo "Error creating tables: " . $conn->error;
}
// Close the connection
$conn->close();
?>