116 lines
3.8 KiB
PHP
116 lines
3.8 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Formulaire PHP Simple</title>
|
|
</head>
|
|
<body>
|
|
<h1>Ajouter une entrée</h1>
|
|
|
|
<form method="POST">
|
|
<label>Texte :</label>
|
|
<input type="text" name="contenu" required>
|
|
<button type="submit">Enregistrer</button>
|
|
</form>
|
|
|
|
<hr>
|
|
|
|
<?php
|
|
// Charger les variables d'environnement depuis un fichier .env
|
|
// ... car oui ce projet est publié sur gitea (git)
|
|
// Dissimule le mot de passe + user ---- me les redemander !
|
|
function loadEnv($path) {
|
|
if (!file_exists($path)) {
|
|
throw new Exception("Fichier .env introuvable : $path");
|
|
}
|
|
|
|
if (!is_readable($path)) {
|
|
throw new Exception("Fichier .env non lisible : $path");
|
|
}
|
|
|
|
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
|
if ($lines === false) {
|
|
throw new Exception("Impossible de lire le fichier .env : $path");
|
|
}
|
|
|
|
foreach ($lines as $line) {
|
|
$line = trim($line);
|
|
|
|
// Ignorer les commentaires et lignes vides
|
|
if (empty($line) || strpos($line, '#') === 0) {
|
|
continue;
|
|
}
|
|
|
|
// Parser la ligne KEY=VALUE
|
|
if (strpos($line, '=') === false) {
|
|
continue;
|
|
}
|
|
|
|
list($key, $value) = explode('=', $line, 2);
|
|
$key = trim($key);
|
|
$value = trim($value);
|
|
|
|
// Retirer les guillemets si présents
|
|
$value = trim($value, '"\'');
|
|
|
|
// Définir la variable d'environnement
|
|
putenv("$key=$value");
|
|
$_ENV[$key] = $value;
|
|
$_SERVER[$key] = $value;
|
|
}
|
|
}
|
|
// Charger le fichier .env (adapter le chemin selon votre structure)
|
|
loadEnv('/media/BC_01C/www/vscode_server/index/assets/mysql/.env');
|
|
// Configuration de la base de données
|
|
$host = getenv('DB_HOST') ?: '127.0.0.1';
|
|
$port = getenv('DB_PORT') ?: '3367';
|
|
$dbname = getenv('DB_NAME') ?: 'nom_base_de_donnes_default';
|
|
$username = getenv('DB_USER');
|
|
$password = getenv('DB_PASSWORD');
|
|
|
|
try {
|
|
// Connexion à MySQL
|
|
$pdo = new PDO("mysql:host=$host;port=$port;dbname=$dbname;charset=utf8", $username, $password);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
// Créer la table si elle n'existe pas
|
|
$createTable = "CREATE TABLE IF NOT EXISTS entrees (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
contenu TEXT NOT NULL,
|
|
date_creation DATETIME NOT NULL
|
|
)";
|
|
$pdo->exec($createTable);
|
|
|
|
// Si le formulaire est soumis
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['contenu'])) {
|
|
$contenu = $_POST['contenu'];
|
|
|
|
// Insertion dans la base
|
|
$stmt = $pdo->prepare("INSERT INTO entrees (contenu, date_creation) VALUES (?, NOW())");
|
|
$stmt->execute([$contenu]);
|
|
|
|
echo "<p><strong>✓ Enregistré avec succès !</strong></p>";
|
|
}
|
|
|
|
// Affichage des entrées
|
|
echo "<h2>Entrées enregistrées :</h2>";
|
|
$stmt = $pdo->query("SELECT * FROM entrees ORDER BY date_creation DESC");
|
|
$entrees = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (count($entrees) > 0) {
|
|
echo "<ul>";
|
|
foreach ($entrees as $entree) {
|
|
echo "<li>" . htmlspecialchars($entree['contenu']) . " <em>(" . $entree['date_creation'] . ")</em></li>";
|
|
}
|
|
echo "</ul>";
|
|
} else {
|
|
echo "<p>Aucune entrée pour le moment.</p>";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
echo "<p><strong>Erreur :</strong> " . $e->getMessage() . "</p>";
|
|
}
|
|
?>
|
|
|
|
</body>
|
|
</html>
|