36952-vm/add_property.php
Flatlogic Bot 76d7d99142 PMS 1
2025-12-15 01:31:18 +00:00

106 lines
4.2 KiB
PHP

<?php
require_once 'session.php';
check_admin();
require_once 'db/config.php';
$name = $address = $rent_amount = '';
$errors = [];
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 = "INSERT INTO properties (name, address, rent_amount) VALUES (:name, :address, :rent_amount)";
$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->execute();
$property_id = $db->lastInsertId(); // Get the ID of the new property
// 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', $property_id, PDO::PARAM_INT);
$file_stmt->execute();
} else {
$errors[] = "Sorry, there was an error uploading your file.";
// Note: The property has been added, but the file upload failed.
// You might want to handle this case, e.g., by showing a specific error message.
}
}
header('Location: properties.php?message=Property added successfully.');
exit;
} catch (Exception $e) {
$errors[] = "DB ERROR: " . $e->getMessage();
}
}
}
include 'templates/header.php';
?>
<div class="container mt-4">
<h1>Add New Property</h1>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="add_property.php" method="POST" enctype="multipart/form-data">
<div class="mb-3">
<label for="name" class="form-label">Property Name</label>
<input type="text" class="form-control" id="name" name="name" value="<?php echo htmlspecialchars($name); ?>" required>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" id="address" name="address" rows="3" required><?php echo htmlspecialchars($address); ?></textarea>
</div>
<div class="mb-3">
<label for="rent_amount" class="form-label">Rent Amount ($)</label>
<input type="number" step="0.01" class="form-control" id="rent_amount" name="rent_amount" value="<?php echo htmlspecialchars($rent_amount); ?>" required>
</div>
<div class="mb-3">
<label for="file" class="form-label">Upload File</label>
<input type="file" class="form-control" id="file" name="file">
</div>
<button type="submit" class="btn btn-primary">Save Property</button>
<a href="properties.php" class="btn btn-secondary">Cancel</a>
</form>
</div>
<?php include 'templates/footer.php'; ?>