<?php
	session_start();
	include_once"includes/conn.php";
	include_once"functions/Success.php";
	include_once"functions/Error.php";
	$id = 'admin';

	if (isset($_POST['Add'])) {
		$username = mysqli_real_escape_string($conn, strip_tags($_POST['uname']));
		$password = mysqli_real_escape_string($conn, strip_tags($_POST['pword']));
		$password1 = mysqli_real_escape_string($conn, strip_tags($_POST['pword1']));
		$key = mysqli_real_escape_string($conn, strip_tags($_POST['key']));
	
		if (empty($username) || empty($password) || empty($password1) || empty($key)) {
			$_SESSION['er'] = 'emp';
			header('Location: register.php');
		}elseif($password !== $password1){
			$_SESSION['er'] = 'pdnm';
			header('Location: register.php');
		}else{
			$sql0 = "SELECT * FROM users";
			$res0 = mysqli_query($conn, $sql0);
			if (mysqli_num_rows($res0)>0) {
				while ($rows0 = mysqli_fetch_assoc($res0)) {
					$uname = $rows0['Username'];
				}
				if ($username === $uname) {
				$_SESSION['er'] = 'une';
				header('Location: register.php');
				}else{
					$sql = "SELECT * FROM users WHERE Key_ ='$key'";
			$result = mysqli_query($conn, $sql);
			if (mysqli_num_rows($result)>0) {
				while ($rows = mysqli_fetch_assoc($result)) {
				
				$key1 = $rows['Key_'];
			}
			if ($key !== $key1) {
				$_SESSION['er'] = 'invkey';
				header('Location: register.php');
				}else{
					$sql1 = "INSERT INTO users(Username, Password) Values('$username', '$password')";
			$res = mysqli_query($conn, $sql1);
			if ($res) {
				$_SESSION['suc'] = 'Added';
                header('Location: register.php?rt=1');
					}
				}

			}
				}
			}
			
		}

	
	}

		
?>


