modifprofil.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <html>
  2. <head>
  3. <title>Modifier votre profil</title>
  4. <link href="custom.css" rel="stylesheet">
  5. <meta name="viewport" content="width=device-width">
  6. <?php
  7. session_start();
  8. if (!isset($_SESSION['loggedin'])) {
  9. header("Location: login.php");
  10. exit;
  11. }
  12. ?>
  13. </head>
  14. <body>
  15. <h1>Modifier votre profil</h1>
  16. <?php
  17. include_once 'class/sqlconnect.php';
  18. try {
  19. $conn = new PDO("mysql:host=$host;dbname=$dbname", $dbusername, $dbpassword);
  20. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  21. $stmt = $conn->prepare("SELECT first_name, last_name, profession, ID, password FROM users WHERE username = :username");
  22. $stmt->bindParam(':username', $_SESSION['username']);
  23. $stmt->execute();
  24. $result = $stmt->fetch(PDO::FETCH_ASSOC);
  25. $prenom = $result['first_name'];
  26. $nom = $result['last_name'];
  27. $profession = $result['profession'];
  28. $idsession = $result['ID'];
  29. } catch (PDOException $e) {
  30. echo "Error: " . $e->getMessage();
  31. }
  32. ?>
  33. <a href="index.php">Revenir à l'accueil</a><br><br>
  34. <form action="modifprofil.php" method="post">
  35. <table>
  36. <tr>
  37. <td>
  38. <input type="hidden" name="ID" size="50" value="<?php echo $result['ID']; ?>">
  39. <label>Prénom :</label>
  40. </td>
  41. <td>
  42. <input type="text" name="first_name" size="50" value="<?php echo $result['first_name']; ?>">
  43. </td>
  44. </tr>
  45. <tr>
  46. <td>
  47. <label>Nom :</label>
  48. </td>
  49. <td>
  50. <input type="text" name="last_name" size="50" value="<?php echo $result['last_name']; ?>">
  51. </td>
  52. <tr>
  53. <td>
  54. <label>Profession (renseigner un seul poste) :</label>
  55. </td>
  56. <td>
  57. <input type="text" name="profession" size="50" value="<?php echo $result['profession']; ?>">
  58. </td>
  59. </tr>
  60. <tr>
  61. <td>
  62. <label for="password">Mot de passe<br>(seulement si vous souhaitez le modifier) :</label>
  63. </td>
  64. <td>
  65. <input type="password" name="password" size="50" value="<?php echo $result['password']; ?>">
  66. </td>
  67. </tr>
  68. </table>
  69. <br>
  70. <input type="submit" name="submit" value="Enregistrer les modifications">
  71. </form>
  72. <?php
  73. include_once 'class/sqlconnect.php';
  74. if ($conn->connect_error) {
  75. die("La connexion à la base de données a échoué : " . $conn->connect_error);
  76. }
  77. if(isset($_POST['submit'])) {
  78. $id = $_POST['ID'];
  79. $prenom = $_POST['first_name'];
  80. $nom = $_POST['last_name'];
  81. $profession = $_POST['profession'];
  82. $password = $_POST['password'];
  83. $sql = "UPDATE users SET first_name='$prenom', last_name='$nom', profession='$profession', password='$password' WHERE ID='$id'";
  84. if ($conn->query($sql) === TRUE) {
  85. echo "Votre profil a été modifié avec succès";
  86. header("refresh:1; url=index.php");
  87. } else {
  88. echo "Votre profil a été modifié avec succès";
  89. header("refresh:1; url=index.php");
  90. }
  91. }
  92. ?>