<?php

session_start();
error_reporting(0);

//intializing variable
$firstname = "";
$lastname = "";
$email = "";
$branch = "";
$password1 = "";
$password2 = "";

$error = array();
$servername = "localhost";
$username = "root";
$pass = "";
$dbname = "webpage";
// Create connection
$db = mysqli_connect($servername, $username, $pass, $dbname);

//Registering user
if(isset($_POST['submit'])){
	$name = mysqli_real_escape_string($db, $_POST['name']);
	$roll = mysqli_real_escape_string($db, $_POST['roll']);
	$email = mysqli_real_escape_string($db, $_POST['email']);
	$branch = mysqli_real_escape_string($db, $_POST['branch']);
	$password1 = mysqli_real_escape_string($db, $_POST['password1']);
	$password2 = mysqli_real_escape_string($db, $_POST['password2']);


	if(empty($name)){array_push($error, "firstname is required");}
	if(empty($roll)) {array_push($error, "lastname is required");}
	if(empty($email)){array_push($error, "email is required");}
	if(empty($branch)){array_push($error, "branch is required");}
	if(empty($password1)){array_push($error, "password1 is required");}
	if(empty($password2)){array_push($error, "password2 is required");}
	 if($password1 != $password2){array_push($error, "password must be same !!");}



	$email_check_query = "SELECT * FROM student WHERE email = '$email' LIMIT 1";
	$result = mysqli_query($db, $email_check_query);
	$user = mysqli_fetch_assoc($result);
	if($user){
		if($user['email'] == $email){
			array_push($error, "Email has already been exits");
			
		}
	}



	if(count($error)==0)
	{
		$password = md5($password1);
		$created = date('Y-m-d H:i:s');
		$inset_query ="INSERT INTO student(name, roll, email,branch,password,created_at) VALUES ('$name','$roll','$email','$branch','$password','$created')";
		mysqli_query($db, $inset_query);
		$_SESSION['email'] = $email;
		$_SESSION['success'] = "You are now logged in";

		header("location: index.php");


	}

}
if(isset($_POST['login_user'])){
	$email = mysqli_real_escape_string($db, $_POST['email']);
	$password = mysqli_real_escape_string($db, $_POST['password']);
	$err =0;
	if(empty($email)){
	 	array_push($error, "Username is required");
	 	header("location: index.php");
	 	$err += 1;

	 }
	if(empty($password)){
		array_push($error, "password is required");
		header("location: index.php");
		$err += 1;
	}

	
		$password = md5($password);
		$query = "SELECT * FROM student WHERE email = '$email' AND password = '$password'";
		$result = mysqli_query($db, $query);
		if(mysqli_num_rows($result) <= 0){
			array_push($error, "Email doesn't exits");
		}

		$row = mysqli_fetch_row($result);
		$id = $row[0];
		$name = $row[1];
		$roll = $row[2];

		
		if(count($error) == 0){
			if(mysqli_num_rows($result) == 1){
				$_SESSION['Logged_email'] = $email;
				$_SESSION['id'] = $id;
				$_SESSION['name'] = $name;
				$_SESSION['roll'] = $roll;
				$_SESSION['loggedin'] = '1';
				echo "Login successfully".$email.$password;
				header("location: mainpage.php");
			}
			
		}else{
			header("location: index.php");
		}
	

}


















?>