prepare("SELECT * FROM properties WHERE id = :id"); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); $property = $stmt->fetch(PDO::FETCH_ASSOC); if (!$property) { header('Location: properties.php'); exit; } $name = $property['name']; $address = $property['address']; $rent_amount = $property['rent_amount']; } catch (PDOException $e) { die("DB ERROR: " . $e->getMessage()); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; $address = $_POST['address'] ?? ''; $rent_amount = $_POST['rent_amount'] ?? ''; if (empty($name)) { $errors[] = 'Name is required'; } if (empty($address)) { $errors[] = 'Address is required'; } if (empty($rent_amount) || !is_numeric($rent_amount)) { $errors[] = 'Valid rent amount is required'; } if (empty($errors)) { $db = db(); try { $sql = "UPDATE properties SET name = :name, address = :address, rent_amount = :rent_amount WHERE id = :id"; $stmt = $db->prepare($sql); $stmt->bindParam(':name', $name, PDO::PARAM_STR); $stmt->bindParam(':address', $address, PDO::PARAM_STR); $stmt->bindParam(':rent_amount', $rent_amount, PDO::PARAM_STR); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); // Handle file upload if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) { $target_dir = "uploads/"; if (!is_dir($target_dir)) { mkdir($target_dir, 0755, true); } $file_name = uniqid() . '-' . basename($_FILES["file"]["name"]); $target_file = $target_dir . $file_name; if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { // Insert file info into database $file_sql = "INSERT INTO files (file_name, file_path, property_id) VALUES (:file_name, :file_path, :property_id)"; $file_stmt = $db->prepare($file_sql); $file_stmt->bindParam(':file_name', $file_name, PDO::PARAM_STR); $file_stmt->bindParam(':file_path', $target_file, PDO::PARAM_STR); $file_stmt->bindParam(':property_id', $id, PDO::PARAM_INT); $file_stmt->execute(); } else { $errors[] = "Sorry, there was an error uploading your file."; } } header('Location: edit_property.php?id=' . $id . '&message=Property updated successfully.'); exit; } catch (Exception $e) { $errors[] = "DB ERROR: " . $e->getMessage(); } } } include 'templates/header.php'; ?>

Edit Property

Cancel

Uploaded Files

prepare("SELECT * FROM files WHERE property_id = :property_id"); $file_stmt->bindParam(':property_id', $id, PDO::PARAM_INT); $file_stmt->execute(); $files = $file_stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { $files = []; echo "

Could not load files: " . $e->getMessage() . "

"; } if (count($files) > 0) { echo ''; } else { echo '

No files uploaded for this property yet.

'; } ?>