-
Notifications
You must be signed in to change notification settings - Fork 0
/
addApartment.php
150 lines (127 loc) · 5.29 KB
/
addApartment.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
<?php
require("session.php");
require "connect.php";
$name = $type = $location = $owner_id = $description = $town = "";
$errors = ["name" => "", "type" => "", "location" => "", "owner_id" => "", "town" => "",];
if (isset($_POST['submit'])) {
if (empty($_POST["name"])) {
$errors["name"] = "The Apartment name is required. <br/>";
} else {
$name = htmlspecialchars($_POST["name"]);
}
if (empty($_POST["type"])) {
$errors["type"] = "The Apartment type is required. <br/>";
} else {
$type = htmlspecialchars($_POST["type"]);
}
if (empty($_POST["location"])) {
$errors["location"] = "The location is required. <br/>";
} else {
$location = htmlspecialchars($_POST["location"]);
}
if (empty($_POST["owner_id"])) {
$errors["owner_id"] = "The Apartment Owner is required. <br/>";
} else {
$owner_id = htmlspecialchars($_POST["owner_id"]);
}
if (empty($_POST["town"])) {
$errors["town"] = "The town is required. <br/>";
} else {
$town = htmlspecialchars($_POST["town"]);
}
$description = htmlspecialchars($_POST["description"]);
if (!array_filter($errors)) {
//there is no error in the form
$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 = "INSERT INTO apartment(name,town,location,owner_id,type,description) VALUES('$name','$town','$location','$owner_id','$type','$description' ) ";
if (mysqli_query($conn, $sql)) {
header("Location: apartments.php");
exit;
} else {
echo "quesry error: " . mysqli_error($conn);
}
}
}
$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 justify-center items-center">
<div class="w-full m-8 ">
<form action="addApartment.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>Add Apartment</b>
</h1>
</div>
<div class="w-full ">
<label for="name" class="label required">
Apartment Name
</label>
<input name="name" type="text" class="input" placeholder="Apartment name" maxlength="50" value="<?php echo $name; ?>" required>
<p class="error_text"><?php echo $errors["name"] ? $errors["name"] : ""; ?> </p>
</div>
<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>
<?php foreach ($apartment_types as $apartment_type) { ?>
<option 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>
<?php foreach ($owners as $owner) { ?>
<option 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" maxlength="100" placeholder="Apartment address" autocomplete="address" 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/apartments.php" class="btn secondary w-auto"> Cancel </a>
<input name="submit" type="submit" class="btn primary " value="Add Apartment">
</div>
</form>
</div>
</div>
<?php include "footer.php"; ?>