<?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=="")
		{
			if($type1!=""){
				if(($type1==".jpg")||($type1==".jpeg")||($type1==".gif")||($type1==".png")){
					$move1 =  move_uploaded_file($_FILES['file1']['tmp_name'],'upload/'.$_POST['image1']);
					if($move1){
						$sql = "UPDATE  product SET 
							name_product	=	'".$_POST['name_product']."',
							id_category		=	'".$_POST['id_category']."',
							price			=	'".$_POST['price']."',
							detail			=	'".$_POST['detail']."',
							insurance		=	'".$_POST['insurance']."' 
						WHERE id_product  	= 	'".$_POST['id_product']."';";
						$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 if($type2!=""){
				if(($type2==".jpg")||($type2==".jpeg")||($type2==".gif")||($type2==".png")){
					$move2 =  move_uploaded_file($_FILES['file2']['tmp_name'],'upload/'.$_POST['image2']);
					if($move2){
							$sql = "UPDATE  product SET 
								name_product	=	'".$_POST['name_product']."',
								id_category		=	'".$_POST['id_category']."',
								price			=	'".$_POST['price']."',
								detail			=	'".$_POST['detail']."',
								insurance		=	'".$_POST['insurance']."' 
							WHERE id_product  	= 	'".$_POST['id_product']."';";
							$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{
				$sql = "UPDATE  product SET 
					name_product	=	'".$_POST['name_product']."',
					id_category		=	'".$_POST['id_category']."',
					price			=	'".$_POST['price']."',
					detail			=	'".$_POST['detail']."',
					insurance		=	'".$_POST['insurance']."' 
				WHERE id_product  	= 	'".$_POST['id_product']."';";
				$data = mysql_query($sql);
				
				if($data){	
					echo "<meta http-equiv=\"refresh\" content=\"0;URL=product.php\" />";
				}else{
					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/'.$_POST['image1']);
				$move2 =  move_uploaded_file($_FILES['file2']['tmp_name'],'upload/'.$_POST['image2']);
				if($move1&&$move2){
						$sql = "UPDATE  product SET 
							name_product	=	'".$_POST['name_product']."',
							id_category		=	'".$_POST['id_category']."',
							price			=	'".$_POST['price']."',
							detail			=	'".$_POST['detail']."',
							insurance		=	'".$_POST['insurance']."' 
						WHERE id_product  	= 	'".$_POST['id_product']."';";
						$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>