Files
D3-A01BibliMVC/bibliotheque.php
2025-09-11 11:25:49 +02:00

58 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
$ip = 'localhost';
$user = 'adminBibli';
$pass = 'mdpBibli';
$database = 'bdbibliotheque';
// Définition de la source des données pour PDO
$dsn = "mysql:host=$ip;dbname=$database;charset=utf8mb4";
// Création de l'objet $dbh, de type PDO, qui est la ressource d'accès à la base
try {
$db = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
die("Erreur de connexion : ".$e->getMessage());
}
// Ajout dun livre
if (!empty($_POST['titre']) && !empty($_POST['auteur']) && !empty($_POST['annee'])) {
$stmt = $db->prepare("INSERT INTO livres (titre, auteur, annee) VALUES (?, ?, ?)");
$stmt->execute([$_POST['titre'], $_POST['auteur'], $_POST['annee']]);
}
// Récupération des livres
$stmt = $db->query("SELECT * FROM livres");
$livres = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Bibliothèque</title>
</head>
<body>
<h1>Gestion de la bibliothèque</h1>
<form method="POST">
<label>Titre : <input type="text" name="titre"></label><br>
<label>Auteur : <input type="text" name="auteur"></label><br>
<label>Année : <input type="number" name="annee"></label><br>
<button type="submit">Ajouter</button>
</form>
<h2>Liste des livres</h2>
<ul>
<?php foreach ($livres as $livre): ?>
<li><?= $livre['titre'] ?> - <?= $livre['auteur'] ?> (<?= $livre['annee'] ?>)</li>
<?php endforeach; ?>
</ul>
</body>
</html>