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.'; } } ?> Ajouter un appartement

Ajouter un appartement





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 "

L'appartement numéro " . $numero_appart . " existe déjà.

"; } 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 "

L'appartement a été ajouté avec succès.

"; } else { echo "Error: " . $sql_insert . "
" . $conn->error; } } // Close the database connection $conn->close(); } ?>