<?php
session_start();
$path="./../../../";
include_once($path."db/conn.php");

$username = $_POST['uname'];  
$password = $_POST['psw'];  
  
	//lai novērstu mysqli injicēšanu  
	$username = stripcslashes($username);  
	$password = stripcslashes($password);  
	$username = mysqli_real_escape_string($conn, $username);  
	$password = mysqli_real_escape_string($conn, $password);  
  
	//sql lai iegūtu lietotāja datus
	//$sql = "select *from login where USER_ID = '$username' and PASSWORD_ID = '$password'";
	$sql = "
	SELECT 
		l.id, 
		u.`user`,
		p.`password`,
		l.joined_date
	FROM 
		manga_login l
	LEFT JOIN 
		manga_username u 
		ON l.user_id = u.id
	LEFT JOIN `manga_password` p
		ON l.password_id = p.id
	WHERE 
		u.`user` = '".$username."'
		AND
		p.`password` = '".$password."'";;
	$result = $conn->query($sql);  
	$row = $result->fetch_array(MYSQLI_ASSOC); 
	$count = mysqli_num_rows($result);  
	  
	//ja lietotājs eksistē pieteikties sistēmai
	if($count == 1){  
		//echo "<h1><center> Login successful </center></h1>";
		
	  	$_SESSION["error1"] = "";
		//$_SESSION['sid']=session_id();
		$_SESSION["uid"] = $row["id"];
		$_SESSION["uuser"] = $row["user"];
		$_SESSION["udate"] = $row["joined_date"];
		header("Location: ./../index.html");
	}  
	//ja lietotājs neiksistē izvadīt ziņojumu
	else{  
		//echo "<h1> Login failed. Invalid username or password.</h1>";
		$_SESSION["error1"] = "Login failed. Invalid username or password.";
		header("Location: ./../index.html");
	}   
$connn -> close();
?>
