-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
202 lines (188 loc) · 7 KB
/
register.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
<?php
require("checksession.php");
if(in_session()){
header("location:profile.php");
die();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/register.css" media="screen" title="no title" charset="utf-8">
<title>Registration</title>
</head>
<body>
<div class="bodyout">
<div class="title">
<div class="wrapper">
<div id="uname">
<div id="titlebox">
<h1 id="title">Hogwarts University</h1>
</div>
</div>
</div>
</div>
<div class="regform">
<div class="formtitle">
<h1>Register!<h1>
</div>
<form action="register.php" method="post" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Full Name" required/>
<br>
<input type="email" name="email" placeholder="Email" required>
<br>
<label class="formlabel">Degree:</label>
<select name="degree" required>
<?php
require('dbconnect.php');
$degquery = 'SELECT degree, dname FROM degree';
$degresult = mysqli_query($con, $degquery);
while($degree = mysqli_fetch_array($degresult)){
echo '<option value="'.$degree['degree'].'">'.$degree['dname'].'</option>';
}
?>
</select>
<br>
<label class="formlabel">Branch:</label>
<select name="branch" required>
<?php
$brquery = 'SELECT dcode, dname FROM department';
$brresult = mysqli_query($con, $brquery);
while($branch = mysqli_fetch_array($brresult)){
echo '<option value="'.$branch['dcode'].'">'.$branch['dname'].'</option>'."\n";
}
?>
</select>
<!-- <br> -->
<label class="formlabel">Date of Birth:</label>
<input type="date" name="dob" required/>
<!-- <br> -->
<label class="formlabel">Gender:</label>
<!-- <div class="radio"> -->
<input type="radio" name="gender" value="m" class="formspan" required checked="checked" />
<label class="genderlabel">Male</label>
<!-- </div> -->
<!-- <div class="radio"> -->
<input type="radio" name="gender" value="f" class="formspan" required/>
<label class="genderlabel">Female</label>
<!-- </div> -->
<!-- <br> -->
<input type="password" name="password" placeholder="Enter a password" id="passwd" onkeyup="checkpasswd()" required/>
<!-- <br> -->
<input type="password" name="password2" placeholder="Retype password" id="repasswd" onkeyup="checkpasswd()" required/>
<div id="errormsg">Passwords do not match!</div>
<!-- <br> -->
<!-- <input type="file" name="image" id="image"><br> -->
<input type="submit" name="register" value="Register!" id="registerbutton">
<div id="loginpage"><a href="login.php"><i>Already registered?</i></a></div>
</form>
</div>
</div>
</body>
<script type="text/javascript">
function checkpasswd(){
$passwd = document.getElementById('passwd').value;
$repasswd = document.getElementById('repasswd').value;
if($passwd != $repasswd){
document.getElementById('errormsg').style.visibility="visible";
}
else {
document.getElementById('errormsg').style.visibility="hidden";
}
}
</script>
</html>
<?php
$required = array('name', 'email', 'degree', 'dob', 'gender', 'password', 'password2');
$error = false;
foreach($required as $field){
if(!isset($_POST[$field]) || empty($_POST[$field])){
$error = true;
break;
}
}
if(isset($_POST['password']) && !empty($_POST['password']) && isset($_POST['password2']) && !empty($_POST['password2'])){
if($_POST['password'] != $_POST['password2']){
$error = true;
}
}
if(!$error){
require('dbconnect.php');
$name = htmlentities(trim($_POST['name']));
$email = htmlentities(trim($_POST['email']));
$degree = htmlentities(trim($_POST['degree']));
$dob = htmlentities(trim($_POST['dob']));
$gender = htmlentities(trim($_POST['gender']));
$password = htmlentities(trim($_POST['password']));
$branch = htmlentities(trim($_POST['branch']));
$encpassword = md5($password);
//Generating registration number
$snoquery = "SELECT count(*) as sno FROM student WHERE branch='$branch'";
$snoresult = mysqli_query($con, $snoquery);
if( mysqli_num_rows($snoresult)==1){
$year = date("Y", time());
$snorow = mysqli_fetch_array($snoresult);
$sno = $snorow['sno'];
$didquery = "SELECT did FROM department WHERE dcode='$branch'";
$didresult = mysqli_query($con, $didquery);
$didrow = mysqli_fetch_array($didresult);
$did= $didrow['did'];
$sno = sprintf("%03d", $sno);
$regno = $year.$did.$sno;
}
//Reg No generated
//Insert details to database
$regquery = "INSERT into student VALUES('$name', '$regno', '$email', '$degree', '$branch', '$dob', '$gender')";
$regquery2 = "INSERT into login VALUES('$regno', '$encpassword')";
$regresult = mysqli_query($con, $regquery);
if($regresult == 1){
$regresult2 = mysqli_query($con, $regquery2);
//Image handling
// if(isset($_FILES['image']) && !empty($_FILES['image'])){
// $file_size =$_FILES['image']['size'];
// $file_tmp =$_FILES['image']['tmp_name'];
// $file_type=$_FILES['image']['type'];
// $tmp = explode('.',$_FILES['image']['name']);
// $file_ext=strtolower(end($tmp));
// $file_name=$regno.'.'.$file_ext;
// $expensions= array("jpeg","jpg","png");
//
// if(in_array($file_ext,$expensions)=== false){
// $errors[]="extension not allowed, please choose a JPEG or PNG file.";
// }
//
// if($file_size > 2048000){
// $errors[]='File size must be less than 200KB';
// }
//
// if(empty($errors)==true){
// move_uploaded_file($file_tmp,"images/uploads/".$file_name);
// }else{
// print_r($errors);
// die();
// }
// }
if($regresult2 == 1){
$_SESSION['regno']=$regno;
echo "
<script type=\"text/javascript\">
alert('You\'ve been successfully registered!');
window.location = \"profile.php\";
</script>
";
}
}
}else{
// if(isset($_POST['submit'])){
if($_SERVER['REQUEST_METHOD']=='POST'){
// echo "Error occured!";
echo "
<script type=\"text/javascript\">
document.getElementById('errormsg').innerHTML=\"Error occured! Please check your details\";
document.getElementById('errormsg').style.visibility=\"visible\";
</script>
";
}
}
?>