noam76

add new appart

May 10th, 2023 (edited)
1,079
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.25 KB | None | 0 0
  1. <?php
  2. // Connexion à la base de données
  3. $dsn = 'mysql:host=localhost;dbname=nom_de_la_base_de_données';
  4. $username = 'nom_utilisateur';
  5. $password = 'mot_de_passe';
  6.  
  7. try {
  8.     $pdo = new PDO($dsn, $username, $password);
  9. } catch (PDOException $e) {
  10.     echo 'Erreur de connexion : ' . $e->getMessage();
  11. }
  12.  
  13. // Vérifier si le formulaire a été soumis
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  15.     $numero_appart = $_POST['numero_appart'];
  16.     $nom_locataire = $_POST['nom_locataire'];
  17.  
  18.     // Vérifier si l'appartement existe déjà dans la table
  19.     $stmt = $pdo->prepare('SELECT COUNT(*) FROM appartements WHERE numero_appart = ?');
  20.     $stmt->execute([$numero_appart]);
  21.     $count = $stmt->fetchColumn();
  22.  
  23.     if ($count > 0) {
  24.         echo 'L\'appartement existe déjà.';
  25.     } else {
  26.         // Insérer l'appartement dans la table
  27.         $stmt = $pdo->prepare('INSERT INTO appartements (numero_appart, nom_locataire) VALUES (?, ?)');
  28.         $stmt->execute([$numero_appart, $nom_locataire]);
  29.         echo 'L\'appartement a été ajouté avec succès.';
  30.     }
  31. }
  32. ?>
  33.  
  34. <!DOCTYPE html>
  35. <html>
  36. <head>
  37.     <title>Ajouter un appartement</title>
  38. </head>
  39. <body>
  40.     <h1>Ajouter un appartement</h1>
  41.     <form method="post">
  42.         <label for="numero_appart">Numéro de l'appartement :</label>
  43.         <input type="text" name="numero_appart" id="numero_appart" required><br><br>
  44. <label for="nom_locataire">Nom et prénom du locataire :</label>
  45. <input type="text" name="nom_locataire" id="nom_locataire" required><br><br>
  46. <input type="submit" value="Enregistrer">
  47. </form>
  48. <?php
  49.     // Check if the form is submitted
  50.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  51.  
  52.         // Define database credentials
  53.         $servername = "localhost";
  54.         $username = "your_username";
  55.         $password = "your_password";
  56.         $dbname = "your_database_name";
  57.  
  58.         // Create a connection to the database
  59.         $conn = new mysqli($servername, $username, $password, $dbname);
  60.  
  61.         // Check if the connection is successful
  62.         if ($conn->connect_error) {
  63.             die("Connection failed: " . $conn->connect_error);
  64.         }
  65.  
  66.         // Retrieve form data
  67.         $numero_appart = $_POST["numero_appart"];
  68.         $nom_locataire = $_POST["nom_locataire"];
  69.  
  70.         // Check if the appartement already exists
  71.         $sql_check = "SELECT * FROM appartements WHERE numero_appart = '$numero_appart'";
  72.         $result_check = $conn->query($sql_check);
  73.  
  74.         if ($result_check->num_rows > 0) {
  75.             // Display an error message if the appartement already exists
  76.             echo "<p style='color:red;'>L'appartement numéro " . $numero_appart . " existe déjà.</p>";
  77.         } else {
  78.             // Insert the form data into the database
  79.             $sql_insert = "INSERT INTO appartements (numero_appart, nom_locataire) VALUES ('$numero_appart', '$nom_locataire')";
  80.  
  81.             if ($conn->query($sql_insert) === TRUE) {
  82.                 echo "<p style='color:green;'>L'appartement a été ajouté avec succès.</p>";
  83.             } else {
  84.                 echo "Error: " . $sql_insert . "<br>" . $conn->error;
  85.             }
  86.         }
  87.  
  88.         // Close the database connection
  89.         $conn->close();
  90.     }
  91. ?>
  92. </body>
  93. </html>
  94.  
Advertisement
Add Comment
Please, Sign In to add comment