<?php
session_start ();

	if($_SESSION['id'] == "") {
		die('Logge dich bitte ein!');
	}
	else {
	
	include('../inc/config.php');
		mysql_select_db('account');
		
		$getuser = "SELECT * from account where login = '" . $_SESSION['id'] . "'";
			$checkuser = mysql_query($getuser);
				$num = mysql_num_rows($checkuser);
			
		if($num > 0) {
		
			$array = mysql_fetch_array($checkuser);
	
			$itemid = $_GET['id'];
			$user = $_SESSION['id'];
			$coins = $_SESSION['coins'];
						
					
					
			$check = "SELECT * from itemshop where id = '" . $itemid . "'";
				$query = mysql_query($check);
					$num = mysql_num_rows($query);
			if($num > 0) {
				$array2 = mysql_fetch_array($query);
			$rescash = $array['coins'] - $array2['price'];
						
		if($num > 0 && $rescash > 0) {
				mysql_select_db('player');
			$i = 0;	
		$giveitem = "INSERT INTO item SET owner_id = '" . $array['id'] . "', window = 'MALL', pos = '" . $_SESSION['pos'] . "', count = '1', vnum = '" . $itemid . "', socket0 = '0', socket1 = '0', socket2 = '0'";
			$result = mysql_query($giveitem);
			
				if($result) {
				
				
				if($_SESSION['pos'] == '42') {
				 
				 $_SESSION['pos'] = '0'; }
				 else { $_SESSION['pos'] = $_SESSION['pos'] + 1; }
				 
					mysql_select_db('account');
					
						$cashresult = $array['coins'] - $array2['price'];
					
					$coins = "UPDATE account set coins = '" . $cashresult . "' where login = '" . $_SESSION['id'] . "'";
						$coinsresult = mysql_query($coins);
						
					if($coinsresult) {
					$_SESSION['coins'] = $cashresult;
					
					echo "<center>Du hast erfolgreich ein/e " . $array2['name'] . " gekauft! <br>
					Du wirst in 3 Sekunden weitergeleitet.
					<meta http-equiv=\"refresh\" content=\"3;url=../index.php?sys=shop\">";
					} else {
					echo "Kaufen fehlgeschlagen.";
					}
				
				}
				else { echo "Kaufen fehlgeschlagen."; }
				
			} else { echo "Du hast zu wenig Coins."; }
			
		} else { echo "Kaufen fehlgeschlagen."; }
		
	} else { echo "Bitte logge dich ein."; }

	}

?>