prepare("SELECT * FROM tenants WHERE id = :id"); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); $tenant = $stmt->fetch(PDO::FETCH_ASSOC); if (!$tenant) { header('Location: index.php?tab=tenants'); exit; } // Fetch properties for dropdown $stmt = $db->query('SELECT id, name FROM properties ORDER BY name'); $properties = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $error = "DB Error: " . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; $phone = $_POST['phone'] ?? ''; $property_id = !empty($_POST['property_id']) ? (int)$_POST['property_id'] : null; $lease_start = !empty($_POST['lease_start']) ? $_POST['lease_start'] : null; $lease_end = !empty($_POST['lease_end']) ? $_POST['lease_end'] : null; $rent_due = !empty($_POST['rent_due']) ? (float)$_POST['rent_due'] : null; $security_deposit = !empty($_POST['security_deposit']) ? (float)$_POST['security_deposit'] : null; $status = $_POST['status'] ?? 'active'; try { $db->beginTransaction(); $sql = "UPDATE tenants SET name = :name, email = :email, phone = :phone, property_id = :property_id, lease_start = :lease_start, lease_end = :lease_end, rent_due = :rent_due, security_deposit = :security_deposit, status = :status WHERE id = :id"; $stmt = $db->prepare($sql); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->bindParam(':name', $name); $stmt->bindParam(':email', $email); $stmt->bindParam(':phone', $phone); $stmt->bindParam(':property_id', $property_id); $stmt->bindParam(':lease_start', $lease_start); $stmt->bindParam(':lease_end', $lease_end); $stmt->bindParam(':rent_due', $rent_due); $stmt->bindParam(':security_deposit', $security_deposit); $stmt->bindParam(':status', $status); $stmt->execute(); if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) { $upload_dir = 'uploads/'; $file_name = uniqid() . '_' . basename($_FILES['file']['name']); $target_file = $upload_dir . $file_name; if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) { $stmt = $db->prepare("INSERT INTO files (file_name, file_path, tenant_id) VALUES (?, ?, ?)"); $stmt->execute([$_FILES['file']['name'], $target_file, $id]); } else { throw new Exception("Failed to upload file."); } } $db->commit(); header('Location: index.php?tab=tenants&message=Tenant updated successfully.'); exit; } catch (Exception $e) { $db->rollBack(); $error = "Error updating tenant: " . $e->getMessage(); } } ?>
No files uploaded for this tenant.