<?php
$title = 'Добавление фотографии товара';
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/head.php');
$_GET['id'] = abs(intval($_GET['id']));
$query = $db->query("SELECT * FROM `products` WHERE `id`='".$_GET['id']."'");
if($query->num_rows==0){
	header('location:/admin');
}
$product = $query->fetch_assoc();
admin(3);
if(isset($_POST['ok'])){
	if(!file_exists($_FILES["image"]["tmp_name"])){
		echo 'Выберите файл!';
	}else{
		$file = pathinfo($_FILES["image"]["name"]);
		$name = $_GET['id'].'_'.($db->query("SELECT `id` FROM `products_images` WHERE `id_product`='".$_GET['id']."'")->num_rows+1).'.'.$file['extension'];
		move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER["DOCUMENT_ROOT"].'/files/images/products/'.$name);
		$db->query("INSERT INTO `products_images` SET `id_product`='".$_GET['id']."', `image`='".$name."', `description`='".$_POST['description']."'");
	}
}
?>
<h1>Добавление изображения к товару <?=output($product['name'])?></h1>
<form action="" method="POST" enctype="multipart/form-data">
	Описание:<br>
	<textarea name="description"></textarea><br>
	Выберите файл:<br>
	<input type="file" name="image"><br>
	<input type="submit" name="ok" value="Загрузить">
</form>
<?
include_once($_SERVER["DOCUMENT_ROOT"].'/php/style/foot.php');
?>