-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_hostel.php
232 lines (190 loc) · 8.39 KB
/
update_hostel.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php include('server.php'); ?>
<div class="main-content">
<div class="wrapper">
<h1>Update Hostel</h1>
<br><br>
<?php
//Check whether the id is set or not
if(isset($_GET['id']))
{
//Get the ID and all other details
//echo "Getting the Data";
$id = $_GET['id'];
//Create SQL Query to get all other details
$sql = "SELECT * FROM tbl_category WHERE id=$id";
//Execute the Query
$res = mysqli_query($db, $sql);
//Count the Rows to check whether the id is valid or not
$count = mysqli_num_rows($res);
if($count==1)
{
//Get all the data
$row = mysqli_fetch_assoc($res);
$title = $row['title'];
$current_image = $row['image_name'];
$featured = $row['featured'];
$active = $row['active'];
}
else
{
//redirect to manage category with session message
$_SESSION['no-category-found'] = "<div class='error'>Category not Found.</div>";
header('location:'.SITEURL.'manage_category.php');
}
}
else
{
//redirect to Manage CAtegory
header('location:'.SITEURL.'manage_hostel.php');
}
?>
<form action="" method="POST" enctype="multipart/form-data">
<table class="tbl-30">
<tr>
<td>Title: </td>
<td>
<input type="text" name="title" value="<?php echo $title; ?>">
</td>
</tr>
<tr>
<td>Current Image: </td>
<td>
<?php
if($current_image != "")
{
//Display the Image
?>
<img src="<?php echo SITEURL; ?>images/category/<?php echo $current_image; ?>" width="150px">
<?php
}
else
{
//Display Message
echo "<div class='error'>Image Not Added.</div>";
}
?>
</td>
</tr>
<tr>
<td>New Image: </td>
<td>
<input type="file" name="image">
</td>
</tr>
<tr>
<td>Featured: </td>
<td>
<input <?php if($featured=="Yes"){echo "checked";} ?> type="radio" name="featured" value="Yes"> Yes
<input <?php if($featured=="No"){echo "checked";} ?> type="radio" name="featured" value="No"> No
</td>
</tr>
<tr>
<td>Active: </td>
<td>
<input <?php if($active=="Yes"){echo "checked";} ?> type="radio" name="active" value="Yes"> Yes
<input <?php if($active=="No"){echo "checked";} ?> type="radio" name="active" value="No"> No
</td>
</tr>
<tr>
<td>
<input type="hidden" name="current_image" value="<?php echo $current_image; ?>">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="submit" name="submit" value="Update Category" class="btn-secondary">
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
//echo "Clicked";
//1. Get all the values from our form
$id = $_POST['id'];
$title = $_POST['title'];
$current_image = $_POST['current_image'];
$featured = $_POST['featured'];
$active = $_POST['active'];
//2. Updating New Image if selected
//Check whether the image is selected or not
if(isset($_FILES['image']['name']))
{
//Get the Image Details
$image_name = $_FILES['image']['name'];
//Check whether the image is available or not
if($image_name != "")
{
//Image Available
//A. UPload the New Image
//Auto Rename our Image
//Get the Extension of our image (jpg, png, gif, etc) e.g. "specialfood1.jpg"
$ext = end(explode('.', $image_name));
//Rename the Image
$image_name = "img".rand(000, 999).'.'.$ext; // e.g. Food_Category_834.jpg
$source_path = $_FILES['image']['tmp_name'];
$destination_path = "images/category/".$image_name;
//Finally Upload the Image
$upload = move_uploaded_file($source_path, $destination_path);
//Check whether the image is uploaded or not
//And if the image is not uploaded then we will stop the process and redirect with error message
if($upload==false)
{
//SEt message
$_SESSION['upload'] = "<div class='error'>Failed to Upload Image. </div>";
//Redirect to Add CAtegory Page
header('location:'.SITEURL.'manage_hostel.php');
//STop the Process
die();
}
//B. Remove the Current Image if available
if($current_image!="")
{
$remove_path = "images/category/".$current_image;
$remove = unlink($remove_path);
//CHeck whether the image is removed or not
//If failed to remove then display message and stop the processs
if($remove==false)
{
//Failed to remove image
$_SESSION['failed-remove'] = "<div class='error'>Failed to remove current Image.</div>";
header('location:'.SITEURL.'manage_hostel.php');
die();//Stop the Process
}
}
}
else
{
$image_name = $current_image;
}
}
else
{
$image_name = $current_image;
}
//3. Update the Database
$sql2 = "UPDATE tbl_category SET
title = '$title',
image_name = '$image_name',
featured = '$featured',
active = '$active'
WHERE id=$id
";
//Execute the Query
$res2 = mysqli_query($conn, $sql2);
//4. REdirect to Manage Category with MEssage
//CHeck whether executed or not
if($res2==true)
{
//Category Updated
$_SESSION['update'] = "<div class='success'>Category Updated Successfully.</div>";
header('location:'.SITEURL.'manage_hostel.php');
}
else
{
//failed to update category
$_SESSION['update'] = "<div class='error'>Failed to Update Category.</div>";
header('location:'.SITEURL.'manage_hostel.php');
}
}
?>
</div>
</div>