-
Notifications
You must be signed in to change notification settings - Fork 0
/
editApartment.php
189 lines (159 loc) · 6.76 KB
/
editApartment.php
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
require("session.php");
require "connect.php";
$name = $type = $location = $owner_id = $description = $town = $apartment_id = "";
$errors = ["name" => "", "type" => "", "location" => "", "owner_id" => "", "town" => ""];
if (isset($_POST['update_apartment']) && isset($_POST['apartment_id']) && !empty($_POST["apartment_id"])) {
$apartment_id = mysqli_real_escape_string($conn, $_POST["apartment_id"]);
if (empty($_POST["name"])) {
$errors["name"] = "Apartment name is required. <br/>";
} else {
$name = htmlspecialchars($_POST["name"]);
}
if (empty($_POST["type"])) {
$errors["type"] = "Apartment type is required. <br/>";
} else {
$type = htmlspecialchars($_POST["type"]);
}
if (empty($_POST["location"])) {
$errors["location"] = "Location is required. <br/>";
} else {
$location = htmlspecialchars($_POST["location"]);
}
if (empty($_POST["owner_id"])) {
$errors["owner_id"] = "Apartment Owner is required. <br/>";
} else {
$owner_id = htmlspecialchars($_POST["owner_id"]);
}
if (empty($_POST["town"])) {
$errors["town"] = "Town is required. <br/>";
} else {
$town = htmlspecialchars($_POST["town"]);
}
$description = htmlspecialchars($_POST["description"]);
if (!array_filter($errors)) {
$name = mysqli_real_escape_string($conn, $_POST["name"]);
$town = mysqli_real_escape_string($conn, $_POST["town"]);
$location = mysqli_real_escape_string($conn, $_POST["location"]);
$owner_id = mysqli_real_escape_string($conn, $_POST["owner_id"]);
$type = mysqli_real_escape_string($conn, $_POST["type"]);
$description = mysqli_real_escape_string($conn, $_POST["description"]);
$sql = "UPDATE apartment
SET name='$name',town='$town',location='$location',owner_id='$owner_id',type='$type',description='$description'
WHERE apartment_id='$apartment_id';";
if (mysqli_query($conn, $sql)) {
header("Location: apartmentDetails.php?id=" . $apartment_id);
exit;
} else {
echo "query error: " . mysqli_error($conn);
}
}
} else {
$apartment_id = mysqli_real_escape_string($conn, $_POST["apartment_id"]);
$sql = "SELECT * FROM apartment WHERE apartment_id='$apartment_id'";
$result = mysqli_query($conn, $sql);
$apartment = mysqli_fetch_assoc($result);
$name = $apartment['name'];
$town = $apartment['town'];
$location = $apartment['location'];
$type = $apartment['type'];
$owner_id = $apartment['owner_id'];
$description = $apartment['description'];
}
$sql = "SELECT owner_id,name FROM owner;";
$result = mysqli_query($conn, $sql);
$owners = mysqli_fetch_all($result, MYSQLI_ASSOC);
$apartment_types = [
["name" => "Flat", "id" => "FLAT"],
["name" => "Bunglow", "id" => "BUNGLOW"],
["name" => "Maisonette", "id" => "MAISONETTE"],
["name" => "Other", "id" => "OTHER"]
]
?>
<?php include "header.php"; ?>
<div class="flex ">
<div class="w-full m-5">
<form action="editApartment.php" method="post" class="max-w-md mx-auto w-full flex flex-col justify-center items-center gap-4 bg-gray-50 rounded-xl p-4 shadow overflow-hidden">
<div class="p-3 text-center">
<h1 class="text-3xl text-gray-700 my-2">
<b>Edit Apartment Details</b>
</h1>
</div>
<div class="w-full">
<label for="name" class="label required">
Apartment Name
</label>
<input name="name" type="text" class="input" maxlength="50" placeholder="Apartment name" value="<?php echo $name; ?>" required>
<p class="error_text"><?php echo $errors["name"] ? $errors["name"] : ""; ?> </p>
</div>
<input name="apartment_id" type="hidden" value="<?php echo $apartment_id; ?>">
<div class="w-full flex gap-4 justify-center items-center">
<div class="w-full ">
<label for="type" class=" label required">
Apartment Type
</label>
<select name="type" class="input" required>
<option disabled <?php if (!isset($type)) {
echo "selected";
} ?>>
------ select type ------
</option>
<?php foreach ($apartment_types as $apartment_type) { ?>
<option <?php if (isset($type) && $type == $apartment_type["id"]) {
echo "selected";
} ?> value="<?php echo $apartment_type['id']; ?>">
<?php echo $apartment_type["name"] ?>
</option>
<?php } ?>
</select>
<p class="error_text"><?php echo $errors["type"] ? $errors["type"] : ""; ?> </p>
</div>
<div class="w-full ">
<label for="owner_id" class="label required">
Apartment Owner
</label>
<select name="owner_id" class="input" required>
<option disabled <?php if (!isset($owner_id)) {
echo "selected";
} ?>>
------ select owner ------
</option>
<?php foreach ($owners as $owner) { ?>
<option <?php if (isset($owner_id) && $owner_id == $owner["owner_id"]) {
echo "selected";
} ?> value="<?php echo $owner['owner_id']; ?>">
<?php echo $owner["name"]; ?>
</option>
<?php } ?>
</select>
<p class="error_text"><?php echo $errors["owner_id"] ? $errors["owner_id"] : ""; ?> </p>
</div>
</div>
<div class="w-full ">
<label for="town" class="label required">
Town
</label>
<input name="town" type="text" class=" input " maxlength="50" placeholder="e.g. Rajkot" value="<?php echo $town; ?>" required>
<p class="error_text"><?php echo $errors["town"] ? $errors["town"] : ""; ?> </p>
</div>
<div class="w-full ">
<label for="location" class=" label required">
Location
</label>
<textarea name="location" class=" input " placeholder="Apartment address" autocomplete="address" maxlength="100" required><?php echo $location; ?></textarea>
<p class="error_text"><?php echo $errors["location"] ? $errors["location"] : ""; ?> </p>
</div>
<div class="w-full ">
<label for="description" class="label">
Description
</label>
<textarea name="description" class=" input " maxlength="100" placeholder="Apartment Description"><?php echo $description; ?></textarea>
</div>
<div class="w-full flex gap-4 ">
<a href="/rms/apartmentDetails.php?id=<?php echo $apartment_id; ?>" class="btn secondary w-auto"> Cancel </a>
<input name="update_apartment" type="submit" class="btn primary" value="Update Apartment Details">
</div>
</form>
</div>
</div>
<?php include "footer.php"; ?>