-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_property.php
More file actions
150 lines (140 loc) · 6.72 KB
/
add_property.php
File metadata and controls
150 lines (140 loc) · 6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
require 'config.php';
if (!isset($_SESSION['user']) || $_SESSION['user']['role'] !== 'landlord') {
header('Location: login.php');
exit();
}
$user = $_SESSION['user'];
$message = '';
if ($_POST) {
$title = trim($_POST['title']);
$description = trim($_POST['description']);
$price = floatval($_POST['price']);
$location = trim($_POST['location']);
$bedrooms = intval($_POST['bedrooms']);
$bathrooms = intval($_POST['bathrooms']);
$size = intval($_POST['size']);
$type = $_POST['type'];
// Insert property
$stmt = $conn->prepare("INSERT INTO properties (landlord_id, title, description, price, location, bedrooms, bathrooms, size_sqft, property_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("issdsiiis", $user['id'], $title, $description, $price, $location, $bedrooms, $bathrooms, $size, $type);
if ($stmt->execute()) {
$property_id = $conn->insert_id;
$message = "<p style='color:green;font-weight:bold;'>Property added successfully!</p>";
// Handle image uploads
if (!empty($_FILES['images']['name'][0])) {
foreach ($_FILES['images']['name'] as $key => $name) {
if ($_FILES['images']['error'][$key] == 0) {
$ext = pathinfo($name, PATHINFO_EXTENSION);
$filename = "prop_" . $property_id . "_" . time() . "_" . $key . "." . $ext;
$path = "uploads/" . $filename;
if (move_uploaded_file($_FILES['images']['tmp_name'][$key], $path)) {
$is_main = ($key == 0) ? 1 : 0;
$img_stmt = $conn->prepare("INSERT INTO property_images (property_id, image_path, is_main) VALUES (?, ?, ?)");
$img_stmt->bind_param("isi", $property_id, $path, $is_main);
$img_stmt->execute();
}
}
}
}
} else {
$message = "<p style='color:red;'>Error adding property.</p>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Property - Apartment Rental</title>
<link rel="stylesheet" href="style.css">
<style>
.form-container { max-width: 800px; margin: 40px auto; padding: 30px; background: white; border-radius: 20px; box-shadow: 0 20px 50px rgba(93,64,55,0.2); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.full-width { grid-column: 1 / -1; }
input, textarea, select { width: 100%; padding: 15px; border: 2px solid #d8c3a5; border-radius: 12px; font-size: 1rem; }
.image-preview { margin: 20px 0; display: flex; gap: 10px; flex-wrap: wrap; }
.image-preview img { width: 100px; height: 100px; object-fit: cover; border-radius: 10px; border: 3px solid #8d6e63; }
</style>
</head>
<body>
<div class="header">
<h1>Add New Property</h1>
<a href="dashboard.php" style="float:left; margin:20px; color:white;">Back to Dashboard</a>
<a href="logout.php" class="logout-btn">Logout</a>
</div>
<div class="container">
<div class="form-container">
<h2 style="text-align:center; color:#6d4c41; margin-bottom:20px;">List Your Property</h2>
<?= $message ?>
<form method="POST" enctype="multipart/form-data">
<div class="form-grid">
<div>
<label>Property Title</label>
<input type="text" name="title" required placeholder="e.g. Cozy 2 Bedroom in Nakawa">
</div>
<div>
<label>Price (UGX)</label>
<input type="number" name="price" required placeholder="500000">
</div>
<div>
<label>Location</label>
<input type="text" name="location" required placeholder="e.g. Nakawa, Kampala">
</div>
<div>
<label>Property Type</label>
<select name="type" required>
<option value="apartment">Apartment</option>
<option value="house">House</option>
<option value="studio">Studio</option>
<option value="room">Single Room</option>
</select>
</div>
<div>
<label>Bedrooms</label>
<input type="number" name="bedrooms" required min="1" value="1">
</div>
<div>
<label>Bathrooms</label>
<input type="number" name="bathrooms" required min="1" value="1">
</div>
<div>
<label>Size (sq ft)</label>
<input type="number" name="size" required placeholder="e.g. 800">
</div>
<div class="full-width">
<label>Description</label>
<textarea name="description" rows="5" required placeholder="Beautiful modern apartment with balcony, near shopping mall..."></textarea>
</div>
<div class="full-width">
<label>Upload Photos (Max 5) - First photo = Main</label>
<input type="file" name="images[]" multiple accept="image/*" required>
<div class="image-preview" id="preview"></div>
</div>
<div class="full-width">
<button type="submit" style="background:#6d4c41; padding:18px; font-size:1.3rem;">Publish Property</button>
</div>
</div>
</form>
</div>
</div>
<script>
document.querySelector('input[type="file"]').onchange = function(e) {
const preview = document.getElementById('preview');
preview.innerHTML = '';
for (let file of e.target.files) {
if (file.type.match('image.*')) {
const reader = new FileReader();
reader.onload = (e) => {
const img = document.createElement('img');
img.src = e.target.result;
preview.appendChild(img);
};
reader.readAsDataURL(file);
}
}
};
</script>
</body>
</html>