Untitled
<?php // Connexion à la base de données $dsn = 'mysql:host=localhost;dbname=formation'; $utilisateur = 'root'; $motDePasse = ''; try { $connexion = new PDO($dsn, $utilisateur, $motDePasse); } catch (PDOException $e) { die("La connexion à la base de données a échoué : " . $e->getMessage()); } // Récupérer la liste des formateurs $stmtFormateurs = $connexion->query("SELECT IDF, nomF,prenomF FROM formateur"); $formateurs = $stmtFormateurs->fetchAll(PDO::FETCH_ASSOC); // Récupérer la liste des cours $stmtCours = $connexion->query("SELECT IDCO, titreCO FROM cours"); $cours = $stmtCours->fetchAll(PDO::FETCH_ASSOC); // Récupérer la liste des participants $stmtParticipants = $connexion->query("SELECT IDP, nomP ,prenomP FROM participant"); $participants = $stmtParticipants->fetchAll(PDO::FETCH_ASSOC); if ($_SERVER["REQUEST_METHOD"] == "POST") { // Récupérer les données du formulaire $idCours = isset($_POST['idCours']) ? $_POST['idCours'] : ''; $idFormateur = isset($_POST['idFormateur']) ? $_POST['idFormateur'] : ''; $idParticipants = isset($_POST['idParticipants']) ? $_POST['idParticipants'] : []; $dateDebut = $_POST['dateDebut']; $dateFin = $_POST['dateFin']; function generateUniqueIDS($database, $length = 6) { $characters = '0123456789'; do { $ids = ''; for ($i = 0; $i < $length; $i++) { $ids .= $characters[rand(0, strlen($characters) - 1)]; } $existingIDSQuery = $database->prepare("SELECT COUNT(*) FROM sessionc WHERE IDS = ?"); $existingIDSQuery->execute([$ids]); $count = $existingIDSQuery->fetchColumn(); } while ($count > 0); return $ids; } function generateUniqueIDPS($database, $length = 6) { $characters = '0123456789'; do { $idps = ''; for ($i = 0; $i < $length; $i++) { $idps .= $characters[rand(0, strlen($characters) - 1)]; } $existingIDPSQuery = $database->prepare("SELECT COUNT(*) FROM sessionp WHERE IDPS = ?"); $existingIDPSQuery->execute([$idps]); $count = $existingIDPSQuery->fetchColumn(); } while ($count > 0); return $idps; } // Générer un IDS unique $ids = generateUniqueIDS($connexion, 6); // Insertion dans la table sessionc $stmtInsertSessionC = $connexion->prepare("INSERT INTO sessionc (IDS, IDCO, IDF, datedS, datefS) VALUES (?, ?, ?, ?, ?)"); $stmtInsertSessionC->execute([$ids, $idCours, $idFormateur, $dateDebut, $dateFin]); // Insertion dans la table sessionp $stmtInsertSessionP = $connexion->prepare("INSERT INTO sessionp (IDPS, IDS, IDP) VALUES (?, ?, ?)"); foreach ($idParticipants as $idParticipant) { $insertionReussie = false; $idps = generateUniqueIDPS($connexion, 6); $stmtInsertSessionP->execute([$idps, $ids, $idParticipant]); $insertionReussie = true; } if ($insertionReussie == true) { header("Location: http://localhost/test web/sessionc.php"); } else { echo "erreur"; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ajouter une session de cours</title> <link rel="stylesheet" href="ajouter.css"> </head> <body> <center> <h1>Ajouter une session de cours</h1> </center> <form action="" method="post"> <label for="idCours">Sélectionnez le cours :</label> <select name="idCours"> <?php foreach ($cours as $cour) : ?> <option value="<?= $cour['IDCO'] ?>"> <?= $cour['titreCO'] ?> </option> <?php endforeach; ?> </select> <br> <label for="idFormateur">Sélectionnez le formateur :</label> <select name="idFormateur"> <?php foreach ($formateurs as $formateur) : ?> <option value="<?= $formateur['IDF'] ?>"> <?= $formateur['nomF'] ?> <?= $formateur['prenomF'] ?> </option> <?php endforeach; ?> </select> <br> <label for="idParticipants">Sélectionnez les participants :</label> <select name="idParticipants[]" multiple id="idParticipantsSelect"> <?php foreach ($participants as $participant) : ?> <option value="<?= $participant['IDP'] ?>"> <?= $participant['nomP'] ?> <?= $participant['prenomP'] ?> </option> <?php endforeach; ?> </select> <br> <script> document.getElementById('idParticipantsSelect').addEventListener('mousedown', function (e) { e.preventDefault(); var originalScrollTop = this.scrollTop; e.target.selected = !e.target.selected; this.focus(); this.scrollTop = originalScrollTop; return false; }); </script> <label for="dateDebut">Date de début :</label> <input type="date" id="dateDebut" name="dateDebut" required> <br> <label for="dateFin">Date de fin :</label> <input type="date" id="dateFin" name="dateFin" required> <br> <input type="submit" value="Ajouter Session"> </form> </body> </html>
Leave a Comment