<html>
<head>
    <meta charset="utf-8"/>
    <title>PRAYATNA - User Registration</title>
    <link rel="stylesheet" href="user_style.css"/>
	<link href="https://fonts.googleapis.com/css2?family=Asap+Condensed&display=swap" rel="stylesheet">
</head>
<body>
<?php
    require('db.php');
    // When form submitted, insert values into the database.
    if (isset($_REQUEST['uname'])) {
        // removes backslashes
        $uname = stripslashes($_REQUEST['uname']);
        //escapes special characters in a string
        $uname = mysqli_real_escape_string($con, $uname);
		$year    = stripslashes($_REQUEST['year']);
        $year    = mysqli_real_escape_string($con, $year);
        $college    = stripslashes($_REQUEST['college']);
        $college    = mysqli_real_escape_string($con, $college);
        $dept = stripslashes($_REQUEST['dept']);
        $dept = mysqli_real_escape_string($con, $dept);
		$email = stripslashes($_REQUEST['email']);
        $email = mysqli_real_escape_string($con, $email);
		$mobile = stripslashes($_REQUEST['mobile']);
        $mobile = mysqli_real_escape_string($con, $mobile);
		$password= stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con, $password);
		$question = stripslashes($_REQUEST['question']);
        $question = mysqli_real_escape_string($con, $question);
		$answer = stripslashes($_REQUEST['answer']);
        $answer = mysqli_real_escape_string($con, $answer);
        $create_datetime = date("Y-m-d H:i:s");
		$query    = "SELECT * FROM `user` WHERE mobile='$mobile'
                     AND password='" . md5($password) . "'";
        $result = mysqli_query($con, $query) or die(mysql_error());
        $rows = mysqli_num_rows($result);
        if ($rows == 0) {
			$query = "INSERT into `user` (uname, year, college, dept, email, mobile, password, question, answer, create_datetime)
						 VALUES ('$uname','$year','$college','$dept','$email','$mobile','" . md5($password) . "','$question','" . md5($answer) . "','$create_datetime')";
			$result   = mysqli_query($con, $query);
			if ($result) {
				echo "<div class='form'>
					  <h3>You are registered successfully.</h3><br/>
					  </div>";
		}
else {
				echo "<div class='form'>
					  <h3>Required fields are missing.</h3><br/>
					  </div>";
            }
?>
	<a href="user_registration.php"> <input type = "button" value = "REGISTER" class="user_login_button"></a>
<?php
		}
		else {
			    echo "<div class='form'>
				<h3>User already exists.</h3><br/>
				</div>";
		}
?>
	<a href="user_login.php"> <input type = "button" value = "LOGIN" class="user_login_button"></a>
<?php

    } else {
?>
<div id="login">

    <form class="user_reg_form" action="" method="post">
        <h1 class="login-title">REGISTRATION</h1>
        <input type="text" id="uname" name="uname" placeholder="Participant Name" required>
		<br><br><br>
        <input type="number" id="year" name="year" placeholder="Year" min="1" max="4" required>
		<br><br><br>
        <input type="text" id="college" name="college" placeholder="College Name" required>
		<br><br><br>
        <select id="dept" name="dept">
		<option id="dept" value="Computer Science and Engineering">Computer Science and Engineering</option>
        <option id="dept" value="Information Technology">Information Technology</option>
        <option id="dept" value="Electronics and Communication Engineering">Electronics and Communication Engineering</option>
		<option id="dept" value="Electronics and Instrumentation Engineering"> Electronics and Instrumentation Engineering</option>
		<option id="dept" value="Mechanical Engineering">Mechanical Engineering</option>
		<option id="dept" value="Aeronautical Engineering">Aeronautical Engineering</option>
		<option id="dept" value="Automobile Engineering">Automobile Engineering</option>
		<option id="dept" value="Production Engineering">Production Engineering</option>
		<option id="dept" value="Rubber and Plastics Technology">Rubber and Plastics Technology</option>
        </select>		
		<br><br><br>
        <input type="email" id="email" name="email" placeholder="Email" required>
		<br><br><br>
        <input type="tel" id="umobile" name="mobile" placeholder="Mobile Number" pattern="[0-9]{10}" required>
		<br><br><br>
        <input type="text" id="upassword" name="password" placeholder="Password" required>
		<br><br><br>
        <input type="text" id="question" name="question" placeholder="Secret Question" required>
		<br><br><br>
        <input type="text" id="answer" name="answer" placeholder="Secret Answer" required>
		<br><br><br>
        <input type="submit" name="submit" value="REGISTER" class="user_login_button">
		<p>Already registered?</p>
        <a href="user_login.php"> <input type = "button" value = "LOGIN" class="user_login_button"></a>
    </form>
	</div>
<?php
    }
?>
</body>
</html>