-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreateuser_student.php
181 lines (158 loc) · 3.79 KB
/
createuser_student.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
<?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['rollNo'])){
$output = array(
"status" => false,
"error" => "Roll No 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['class'])){
$output = array(
"status" => false,
"error" => "Class is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['division'])){
$output = array(
"status" => false,
"error" => "Division is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['stream'])){
$output = array(
"status" => false,
"error" => "Stream is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['address'])){
$output = array(
"status" => false,
"error" => "Address is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['email'])){
$output = array(
"status" => false,
"error" => "E-mail is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
if(!isset($_POST['dob'])){
$output = array(
"status" => false,
"error" => "Date-Of-Birth is not set",
"errorCode" => "102",
"response" => ""
);
die(json_encode($output));
}
//assign the values to local variables
$rollNo=$_POST['rollNo'];
$name=$_POST['name'];
$class=$_POST['class'];
$mobile=$_POST['mobile'];
$division=$_POST['division'];
$stream=$_POST['stream'];
$address=json_encode($_POST['address']);
$email=$_POST['email'];
$dob=$_POST['dob'];
//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));
}
//check if the user already exists
$clash = false;
$student = mysql_query("SELECT `rollNo`, `name`, `mobile`, `class`, `division`, `stream`, `lastLogin`, `address`, `email`, `dob` FROM `dash_students` WHERE `mobile`='{$mobile}' AND `name`='{$name}'");
while($row = mysql_fetch_assoc($student)){
$clash=true;
$output = array(
"status" => false,
"error" => "User already Exists",
"errorCode" => "201",
"response" => ""
);
die(json_encode($output));
}
//add student to database
if(!$clash){
mysql_query("INSERT INTO `dash_students`(`rollNo`, `name`, `mobile`, `class`, `division`, `stream`, `lastLogin`, `address`, `email`, `dob`) VALUES ('{$rollNo}' , '{$name}' , '{$mobile}' , '{$class}' , '{$division}' , '{$stream}' , '' , '{$address}' , '{$email}' , '{$dob}')");
$response = array(
"message" => "Student was created sucessfully"
);
$output = array(
"status" => true,
"error" => "",
"errorCode" => "",
"response" => $response
);
echo json_encode($output);
}
?>