<?php session_start(); 
	include('conn.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
	$date1 = date("U");
	if($_POST['id_category']!=""&&$_POST['name_product']!=""&&$_POST['detail']!=""&&$_POST['price']!=""&&$_POST['insurance']!=""){
		$file1 = strtolower($_FILES["file1"]["name"]);
		$type1= strrchr($file1,".");
		$file2 = strtolower($_FILES["file2"]["name"]);
		$type2= strrchr($file2,".");
		if($type1==""||$type2=="")
		{
			echo"<script>
			alert('กรุณาเลือกรูป');
			history.back();
			</script>";
		}else
		{
			if((($type1==".jpg")||($type1==".jpeg")||($type1==".gif")||($type1==".png"))&&(($type2==".jpg")||($type2==".jpeg")||($type2==".gif")||($type2==".png"))){
				$move1 =  move_uploaded_file($_FILES['file1']['tmp_name'],'upload/a'.$date1.$type1);
				$move2 =  move_uploaded_file($_FILES['file2']['tmp_name'],'upload/b'.$date1.$type2);
				if($move1&&$move2){
					
						$sql = "insert into product (name_product,id_category,price,detail,image1,image2,insurance)values 
													('".$_POST['name_product']."','".$_POST['id_category']."', '".$_POST['price']."', '".$_POST['detail']."','a".$date1.$type1."','b".$date1.$type2."','".$_POST['insurance']."');";
						$data = mysql_query($sql);
						
						if($data){	
							echo "<meta http-equiv=\"refresh\" content=\"0;URL=product.php\" />";
						}else{
							echo"<script>
							alert('เกิดข้อผิดพลาดบันทึกข้อมูล ');
							history.back();
							</script>";
						}	
				}else{
					echo"<script>
					alert('เกิดข้อผิดพลาดอัพรูป ');
					history.back();
					</script>";
				}
			}else{
				echo"<script>
				alert('รูปเฉพาะ .jpg,.jpeg,.gif,.png เท่านั้น ');
				history.back();
				</script>";
			}
		}
	}else{
		echo"<script>
		alert('ข้อมูลยังไม่ครบถ้วน');
		history.back();
		</script>";
	}
?>
</body>
</html>