Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Connexion à la base de données
- $dsn = 'mysql:host=localhost;dbname=nom_de_la_base_de_données';
- $username = 'nom_utilisateur';
- $password = 'mot_de_passe';
- try {
- $pdo = new PDO($dsn, $username, $password);
- } catch (PDOException $e) {
- echo 'Erreur de connexion : ' . $e->getMessage();
- }
- // Vérifier si le formulaire a été soumis
- if ($_SERVER['REQUEST_METHOD'] === 'POST') {
- $numero_appart = $_POST['numero_appart'];
- $nom_locataire = $_POST['nom_locataire'];
- // Vérifier si l'appartement existe déjà dans la table
- $stmt = $pdo->prepare('SELECT COUNT(*) FROM appartements WHERE numero_appart = ?');
- $stmt->execute([$numero_appart]);
- $count = $stmt->fetchColumn();
- if ($count > 0) {
- echo 'L\'appartement existe déjà.';
- } else {
- // Insérer l'appartement dans la table
- $stmt = $pdo->prepare('INSERT INTO appartements (numero_appart, nom_locataire) VALUES (?, ?)');
- $stmt->execute([$numero_appart, $nom_locataire]);
- echo 'L\'appartement a été ajouté avec succès.';
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Ajouter un appartement</title>
- </head>
- <body>
- <h1>Ajouter un appartement</h1>
- <form method="post">
- <label for="numero_appart">Numéro de l'appartement :</label>
- <input type="text" name="numero_appart" id="numero_appart" required><br><br>
- <label for="nom_locataire">Nom et prénom du locataire :</label>
- <input type="text" name="nom_locataire" id="nom_locataire" required><br><br>
- <input type="submit" value="Enregistrer">
- </form>
- <?php
- // Check if the form is submitted
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- // Define database credentials
- $servername = "localhost";
- $username = "your_username";
- $password = "your_password";
- $dbname = "your_database_name";
- // Create a connection to the database
- $conn = new mysqli($servername, $username, $password, $dbname);
- // Check if the connection is successful
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- // Retrieve form data
- $numero_appart = $_POST["numero_appart"];
- $nom_locataire = $_POST["nom_locataire"];
- // Check if the appartement already exists
- $sql_check = "SELECT * FROM appartements WHERE numero_appart = '$numero_appart'";
- $result_check = $conn->query($sql_check);
- if ($result_check->num_rows > 0) {
- // Display an error message if the appartement already exists
- echo "<p style='color:red;'>L'appartement numéro " . $numero_appart . " existe déjà.</p>";
- } else {
- // Insert the form data into the database
- $sql_insert = "INSERT INTO appartements (numero_appart, nom_locataire) VALUES ('$numero_appart', '$nom_locataire')";
- if ($conn->query($sql_insert) === TRUE) {
- echo "<p style='color:green;'>L'appartement a été ajouté avec succès.</p>";
- } else {
- echo "Error: " . $sql_insert . "<br>" . $conn->error;
- }
- }
- // Close the database connection
- $conn->close();
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment