End of the TP8

This commit is contained in:
2023-11-21 14:54:51 +01:00
parent f5596e9133
commit c87986b37a
3 changed files with 205 additions and 59 deletions

View File

@@ -57,6 +57,7 @@
$siecleid = $result['siecleid'];
$statement = $db->query("SELECT nom, prenom FROM auteur WHERE id = $auteurid");
$result = $statement->fetch(PDO::FETCH_ASSOC);
console_log($result);
$nom = $result['nom'];
$prenom = $result['prenom'];
$statement = $db->query("SELECT numero FROM siecle WHERE id = $siecleid");
@@ -66,47 +67,146 @@
echo "<p> $prenom $nom ($numero<sup>e</sup> siècle)</p>";
}
function getAuthorAndSiecle(PDO $db, $author, $siecle){
// Requête préparée pour récupérer l'id de l'auteur
$statement = $db->prepare("SELECT id FROM auteur WHERE nom = :author");
$statement->bindParam(':author', $author);
function console_log($message, $with_script_tags = true){
$js_code = 'console.log(' . json_encode($message, JSON_HEX_TAG) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '</script>';
}
echo $js_code;
}
function getAuthorIdByName(PDO $db, $name){
$statement = $db->prepare("SELECT id FROM auteur WHERE CONCAT(nom, ' ', prenom) = :name");
$statement->bindParam(':name', $name);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result ? $result['id'] : null;
}
// Vérifier si la requête a retourné des résultats
if ($result !== false) {
$auteurid = $result['id'];
function getCenturyIdByNumber(PDO $db, $number){
$statement = $db->prepare("SELECT id FROM siecle WHERE numero = :number");
$statement->bindParam(':number', $number);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result ? $result['id'] : null;
}
// Requête préparée pour récupérer l'id du siècle
$statement = $db->prepare("SELECT id FROM siecle WHERE numero = :siecle");
$statement->bindParam(':siecle', $siecle);
function getQuoteByAuthorAndCentury($db, $author, $century){
try{
$authorId = getAuthorIdByName($db, $author);
$centuryId = getCenturyIdByNumber($db, $century);
$statement = $db->prepare("SELECT phrase FROM citation WHERE auteurid = :authorId AND siecleid = :centuryId");
$statement->bindParam(':authorId', $authorId);
$statement->bindParam(':centuryId', $centuryId);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
// Vérifier si la requête a retourné des résultats
if ($result !== false) {
$siecleid = $result['id'];
// Requête préparée pour récupérer la phrase
$statement = $db->prepare("SELECT phrase FROM citation WHERE auteurid = :authorid AND siecleid = :siecleid");
$statement->bindParam(':authorid', $auteurid);
$statement->bindParam(':siecleid', $siecleid);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
// Vérifier si la requête a retourné des résultats
if ($result !== false) {
$phrase = $result['phrase'];
echo "<p><strong> $phrase </strong></p>";
echo "<p> $author ($siecle<sup>e</sup> siècle)</p>";
} else {
echo "<p>Aucune phrase trouvée pour cet auteur et ce siècle.</p>";
}
} else {
echo "<p>Aucun siècle trouvé pour le numéro spécifié.</p>";
if($result){
return $result;
}
else{
return null;
}
}
catch (PDOException $exception){
error_log('Request error: '.$exception->getMessage());
return false;
}
}
function displayQuoteByAuthorAndCentury(PDO $db, $author, $century){
$quotes = getQuoteByAuthorAndCentury($db, $author, $century);
if($quotes != null){
echo "<table>";
echo "<thead><tr><th>Citation</th></tr></thead>";
echo "<tbody>";
foreach ($quotes as $quote) {
echo "<tr><td>" . $quote['phrase'] . "</td></tr>";
}
echo "</tbody>";
echo "</table>";
}
else{
echo "<p>Aucune citation trouvée</p>";
}
}
function dbGetQuotesID(PDO $db){
$pgsql = "SELECT id FROM citation";
$stmt = $db->prepare($pgsql);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function addQuotesToDb(PDO $db, $authorName, $authorFirstName, $century, $quote){
try {
$db->beginTransaction();
// Check if the author already exists; if not, add it to the database
$authorId = getAuthorIdByName($db, $authorName . " " . $authorFirstName);
if($authorId == null){
$insertAuthorStatement = $db->prepare("INSERT INTO auteur (nom, prenom) VALUES (:nom, :prenom)");
$insertAuthorStatement->bindParam(':nom', $authorName);
$insertAuthorStatement->bindParam(':prenom', $authorFirstName);
$insertAuthorStatement->execute();
$authorId = getAuthorIdByName($db, $authorName . " " . $authorFirstName);
}
// Check if the century already exists; if not, add it to the database
$centuryId = getCenturyIdByNumber($db, $century);
if($centuryId == null){
$insertCenturyStatement = $db->prepare("INSERT INTO siecle (numero) VALUES (:numero)");
$insertCenturyStatement->bindParam(':numero', $century);
$insertCenturyStatement->execute();
$centuryId = getCenturyIdByNumber($db, $century);
}
// Add the quote to the database
$insertQuoteStatement = $db->prepare("INSERT INTO citation (phrase, auteurid, siecleid) VALUES (:phrase, :auteurid, :siecleid)");
$insertQuoteStatement->bindParam(':phrase', $quote);
$insertQuoteStatement->bindParam(':auteurid', $authorId);
$insertQuoteStatement->bindParam(':siecleid', $centuryId);
$insertQuoteStatement->execute();
// Commit the transaction
$db->commit();
} catch (Exception $e) {
// Roll back the transaction if an exception occurs
$db->rollBack();
echo "Failed: " . $e->getMessage();
}
}
function deleteQuoteFromDb(PDO $db, $id){
try {
$db->beginTransaction();
// Delete the quote from the database
$deleteQuoteStatement = $db->prepare("DELETE FROM citation WHERE id = :id");
$deleteQuoteStatement->bindParam(':id', $id);
$deleteQuoteStatement->execute();
// Commit the transaction
$db->commit();
} catch (Exception $e) {
// Roll back the transaction if an exception occurs
$db->rollBack();
echo "Failed: " . $e->getMessage();
}
}
function checkIfFormIsSubmit($db){
// Check if the form is submitted and the get is not empty
if(isset($_GET['AuthorName']) && isset($_GET['AuthorFirstName']) && isset($_GET['Century']) && isset($_GET['Quote'])){
// Check if the fields are not empty
if(!empty($_GET['AuthorName']) && !empty($_GET['AuthorFirstName']) && !empty($_GET['Century']) && !empty($_GET['Quote'])){
// Add the quote to the database
addQuotesToDb($db, $_GET['AuthorName'], $_GET['AuthorFirstName'], $_GET['Century'], $_GET['Quote']);
}
} else {
echo "<p>Aucun auteur trouvé pour le nom spécifié.</p>";
}
}
?>

View File

@@ -1,29 +1,83 @@
<?php
require_once('database.php');
// Enable all warnings and errors.
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Database connection.
$db = dbConnect();
if(isset($_GET['submitAdd'])){
checkIfFormIsSubmit($db);
}
if(isset($_GET['submitDelete'])){
deleteQuoteFromDb($db, $_GET['quotes']);
}
?>
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>TP8-PHP-Marquet</title>
<link rel="icon" href="ISEN-Nantes.png" type="image/x-icon">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="citations.php">Infomations</a>
</li>
<li class="nav-item">
<a class="nav-link" href="recherche.php">Recherche</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="modification.php">Modification</a>
</li>
</ul>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" aria-current="page" href="citations.php">Infomations</a>
</li>
<li class="nav-item">
<a class="nav-link" href="recherche.php">Recherche</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="modification.php">Modification</a>
</li>
</ul>
</div>
</div>
</div>
</nav>
</nav>
<!-- When this form is submitted call the addQuotesToDb function -->
<form action="modification.php" method="get" class="m-3">
<h1>Ajout</h1>
<div clas="mb-3">
<label for="AuthorName" class="form-label">Nom de l'auteur</label>
<input type="text" class="form-control" id="AuthorName" name="AuthorName">
</div>
<div class="mb-3">
<label for="AuthorFirstName" class="form-label">Prénom de l'auteur</label>
<input type="text" class="form-control" id="AuthorFirstName" name="AuthorFirstName">
</div>
<div class="mb-3">
<label for="Century" class="form-label">Siècle</label>
<input type="number" class="form-control" id="Century" name="Century">
</div>
<div class="mb-3">
<label for="Quote" class="form-label">Citation</label>
<input type="text" class="form-control" id="Quote" name="Quote">
</div>
<button type="submit" name="submitAdd" class="btn btn-secondary">Ajouter</button>
</form>
<form action="modification.php" method="get" class="m-3">
<h1>Suppression</h1>
<div class="mb-3">
<select id="quotes" name="quotes" class="form-select">
<option selected>Selectionnez l'ID d'une citation</option>
<?php
$quotes = dbGetQuotesID($db);
foreach ($quotes as $quote) {
echo "<option>" . $quote['id'] . "</option>";
}
?>
</select>
</div>
<button type="submit" name="submitDelete" class="btn btn-secondary">Supprimer</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
</body>
</html>

View File

@@ -62,15 +62,7 @@
if(isset($_POST['auteur']) && isset($_POST['siecle'])){
$author = $_POST['auteur'];
$century = $_POST['siecle'];
$quotes = getAuthorAndSiecle($db, $author, $century);
if($quotes != null){
foreach ($quotes as $quote) {
echo "<p>" . $quote['phrase'] . "</p>";
}
}
else{
echo "<p>Aucune citation trouvée</p>";
}
displayQuoteByAuthorAndCentury($db, $author, $century);
}
?>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>