forked from LahiruSen/OOSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l_scholarship_submission.php
250 lines (172 loc) · 8.07 KB
/
l_scholarship_submission.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
233
234
235
236
237
238
239
240
241
<?php
require 'db.php';
/* Displays user information and some useful messages */
if (session_status() == PHP_SESSION_NONE) { session_start();}
// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
$_SESSION['message'] = "You must log in before viewing your profile page!";
header("location: error.php");
}
else {
// Makes it easier to read
$first_name = $_SESSION['first_name'];
$last_name = $_SESSION['last_name'];
$email = $_SESSION['email'];
$active = $_SESSION['active'];
$types = $_SESSION['types'];
$two_step = $_SESSION['two_step'];
}
if($types == 1)
{
$_SESSION['message'] = "Only students can apply for scholarships !";
header("location: error.php");
die();
}
//content
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// define variables and set to empty values
$nameErr;
$regErr;
$fileErr;
$listErr;
$first_name1 = $registration_number1 ='';
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if ($_POST["list"]=='0') {
$listErr = "Please select the scholarship type";
} else {
$scholarship = test_input($_POST["list"]);
}
if (empty($_POST["first_name"])) {
$nameErr = "Name is required";
} else {
$first_name1 = test_input($_POST["first_name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/", $first_name1)) {
$nameErr = "Only letters and white space allowed for name";
}
}
if (empty($_POST["registration_number"])) {
$regErr = "Registration Number is required.";
} else {
$registration_number1 = test_input($_POST["registration_number"]);
//check if registration number only contains letters and numbers
if (!preg_match("/^[a-zA-Z0-9 ]*$/", $registration_number1)) {
$regErr = "Registration number only can contain letters and numbers";
}
}
// if (empty($_POST["scholarship_application"])) {
// $fileErr = "Please select a pdf file to upload.";}
if (!empty($nameErr) or !empty($regErr) or !empty($fileErr) or!empty($listErr)) {
$_SESSION['message'] = $nameErr . '<br>' . $regErr.'<br>'.$fileErr.'<br>'.$listErr;
header("location: error.php");
die();
}
$email = $_SESSION['email'];
$result = $mysqli->query("SELECT id FROM users WHERE email='$email'");
if ( $result->num_rows == 0 ) // User doesn't exist
{
$_SESSION['message'] = "This user detail doesn't exist in the system.";
header("location: error.php");
die();
}
else { // User exists (num_rows != 0)
$user = $result->fetch_assoc(); // $user becomes array with user data
$user_id = $user['id'];
$result_registration_number = $mysqli->query("SELECT registration_number FROM student_data WHERE user_id='$user_id' " );
if($result_registration_number->num_rows==0){
$_SESSION['message']="This student's details doesn't exist in the system.";
header("location: error.php");
die();
}
else{
$target_dir = "scholarship_application_uploads/";
$target_file = $target_dir . basename($_FILES["scholarship_application"]["name"]);
$uploadOk = 1;
$FileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// supporting code to move file to the directry
$temp = explode(".", $_FILES["scholarship_application"]["name"]);
$newfilename = round(microtime(true)) . '.' . end($temp);
$registration_number_array = $result_registration_number->fetch_assoc(); // employ become arry with employ data
$registration_number = $registration_number_array['registration_number'];
$pdf_url= $target_dir.$newfilename;
$date_of_create= $mysqli->escape_string( date("Y-m-d H:i:s"));
$date_of_update= $mysqli->escape_string( date("Y-m-d H:i:s"));
// write here code to identyfy this registration number has season email.
// Check if pdf file is a actual or not
//if(isset($_POST["submit"])) {
// $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
// if($check !== false) {
// echo "File is an image - " . $check["mime"] . ".";
// $uploadOk = 1;
// } else {
// echo "File is not an image.";
// $uploadOk = 0;
// }
//}
// Check if file already exists
if($registration_number!=$registration_number1){
$_SESSION['message'] = "Wrong Index Number. Please Check again.";
header("location: error.php");
die();
}
elseif (file_exists($target_file)) {
$_SESSION['message'] = "The file already exists!";
$uploadOk = 0;
header("location: error.php");
die();
} // Check file size
else if ($_FILES["scholarship_application"]["size"] > 10000000) {
$_SESSION['message'] = "Maximum size exceeds. Maximum size is 10 MB";
$uploadOk = 0;
header("location: error.php");
die();
} // Allow certain file formats
else if ($FileType != "pdf") {
$_SESSION['message'] = "Sorry, only pdf files are allowed.";
$uploadOk = 0;
header("location: error.php");
die();
} // Check if $uploadOk is set to 0 by an error
else if ($uploadOk == 0) {
$_SESSION['message'] = "Sorry your file was not uploaded";
header("location: error.php");
die();
} // if everything is ok, try to upload file
else {
$result_already_exists = $mysqli->query("SELECT * FROM scholarship_submissions WHERE registration_number='$registration_number' AND scholarship_id='$scholarship'" );
if($result_already_exists->num_rows==0) {
if (move_uploaded_file($_FILES["scholarship_application"]["tmp_name"], 'scholarship_application_uploads/' . $newfilename)) {
$sql = "INSERT INTO scholarship_submissions (registration_number, pdf_url, scholarship_id, date_of_create,date_of_update) "
. "VALUES ('$registration_number','$pdf_url','$scholarship','$date_of_create','$date_of_update')";
if ($mysqli->query($sql)) {
$_SESSION['message'] = "Your scholarship application is uploaded successfully";
header("location: success.php");
$uploadOk = 1;
die();
} else {
$_SESSION['message'] = "Sorry. Your scholarship application could not be uploaded";
header("location: error.php");
die();
}
} else {
$_SESSION['message'] = "Sorry. Error occoured during upload. (During moving file)";
header("location: error.php");
die();
}
}
else{
$_SESSION['message'] = "You have already uploaded an application for this scholarship.";
header("location: error.php");
die();
}
}
}
}
}
?>