-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateuser_teacher.php
158 lines (138 loc) · 3.35 KB
/
createuser_teacher.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
<?php
error_reporting(0);
//Database Connection
define('INCLUDE_CHECK', true);
require 'connect.php';
//json decode the values recieved
$_POST = json_decode(file_get_contents('php://input'), true);
//Params passed or not
if(!isset($_POST['employeeId'])){
$output = array(
"status" => false,
"error" => "Employee ID is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['name'])){
$output = array(
"status" => false,
"error" => "Name is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['mobile'])){
$output = array(
"status" => false,
"error" => "mobile is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['password'])){
$output = array(
"status" => false,
"error" => "password is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['email'])){
$output = array(
"status" => false,
"error" => "email is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['subjects'])){
$output = array(
"status" => false,
"error" => "subjects is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
//assign the values to local variables
$employeeId=$_POST['employeeId'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$subjects=json_encode($_POST['subjects']);
$email=$_POST['email'];
$password=$_POST['password'];
//params validation
//email-id
if(!preg_match("/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/", $email)){
$output = array(
"status" => false,
"error" => "E-mail ID is invalid",
"errorCode" => "101",
"response" => ""
);
die(json_encode($output));
}
//mobile no
if(!preg_match("(^(\+\d{1,3}[- ]?)?\d{10}$)", $mobile)){
$output = array(
"status" => false,
"error" => "Mobile No is invalid",
"errorCode" => "101",
"response" => ""
);
die(json_encode($output));
}
//name
if(!preg_match("(^[A-Za-z\s]{1,}[\.]{0,1}[A-Za-z\s]{0,}$)", $name)){
$output = array(
"status" => false,
"error" => "Name is invalid",
"errorCode" => "101",
"response" => ""
);
die(json_encode($output));
}
//password
if(!preg_match("((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})", $password)){
$output = array(
"status" => false,
"error" => "Password is invalid",
"errorCode" => "101",
"response" => ""
);
die(json_encode($output));
}
//check if the user already exists
$clash = false;
$teacher = mysql_query("SELECT `employeeId`, `name`, `mobile`, `subjects`, `lastLogin`, `password`, `email` FROM `dash_teachers` WHERE `employeeId`='{$employeeId}'");
while($row = mysql_fetch_assoc($teacher)){
$clash=true;
$output = array(
"status" => false,
"error" => "User already Exists",
"errorCode" => "201",
"response" => ""
);
die(json_encode($output));
}
//add teacher to database
if(!$clash){
mysql_query("INSERT INTO `dash_teachers`(`employeeId`, `name`, `mobile`, `subjects`, `lastLogin`, `password`, `email`) VALUES ('{$employeeId}','{$name}','{$mobile}','{$subjects}','','{$password}','{$email}')");
$response = array(
"message" => "teacher was created sucessfully"
);
$output = array(
"status" => true,
"error" => "",
"errorCode" => "",
"response" => $response
);
echo json_encode($output);
}
?>