<?php
require_once('../../include/class.inc.php');
	
	$title=htmlspecialchars($_POST["title"]);
	$Product_id=htmlspecialchars($_POST["Product_id"]);
	if (isset($Product_id)) {
	$Product_id="'$Product_id'";
	}else {
	$Product_id="NULL";
	}
	$fid=intval($_POST["fid"]);
	$spec=htmlspecialchars($_POST["spec"]);
	
	if (isset($spec)) {
	$spec="'$spec'";
	}else {
	$spec="'NULL'";
	}
	$price=htmlspecialchars($_POST["price"]);
	if (isset($price)) {
	$price="'$price'";
	}else {
	$price="'NULL'";
	}
	$content=htmlspecialchars($_POST["content"]);
	$Home_t=$_POST["Home_t"];
	$hits=intval($_POST["hits"]);
	$user=htmlspecialchars($_POST["user"]);
	if (isset($user)) {
	$user="'$user'";
	}else {
	$user="'NULL'";
	}
	$ppt=intval($_POST["ppt"]);

		if ($_POST['submitted'] == "yes"){
				$goodtogo = true;
				//检查图片是否是0长度.
				try {
					if ($_FILES['image']['size'] == 0){
						$goodtogo = false;
						throw new exception ("对不起,不能上传其它格式图片.");
					}
				} catch (exception $e) {
					echo $e->getmessage();
				}
				//检查文件大小.
				try {
					if ($_FILES['image']['size'] > 5000000){
						$goodtogo = false;
						//显示错误信息.
						throw new exception ("对不起,图片文件不能超过: " . intval ($_FILES['image']['size'] / 1000) . "KB");
					}
				} catch (exception $e) {
					echo $e->getmessage();
				}
				//允许上传的mime类型,本例中只允许jpg和gif格式,您可自行添加
				$allowedmimes = array ("image/jpeg","image/pjpeg","image/gif");
				try {
					if (!in_array ($_FILES['image']['type'],$allowedmimes)){
						$goodtogo = false;
						throw new exception ("错误,文件类型必须为 .jpg或是 .gif文件. 我们上传的文件类型为: " . $_FILES['image']['type'] . "");
					}
				} catch (exception $e) {
					echo $e->getmessage ();
				}
				//如果通过提交,则移动到目录中,显示之.
				if ($goodtogo){
					try {
						$pic=$_FILES['image']['name'];	//上传文件总终变量，路径+文件名					
						$date=date('Ymdhis');  //取当前时间
						$name=explode('.',$pic);//将文件名以'.'分割得到后缀名,得到一个数组 
						$newPath=$date.'.'.$name[1]; //得到一个新的文件为'20070705163148.jpg',即新的路径 
						$picName="uploads/".$newPath;	
						//$oldPath=$_FILES['file']['tmp_name'];//临时文件夹,即以前的路径 
						//rename($oldPath,$newPath); 就可以重命名了!							
							if (!move_uploaded_file ($_FILES['image']['tmp_name'],"../uploads/".$newPath)){
								$goodtogo = false;
								throw new exception ("上传文件的uploads目录有错误,请检查属性.");
							}
					} 
						catch (exception $e) {
							echo $e->getmessage ();
						}
				}

		
			}		
			
	if($title!="" && $content!="" && $picName!=""){
  		$sql="insert into product(yiid,title,Product_id,spec,price,content,Home_t,hits,user,adddate,img,ppt,typeid) 
			  values(2,'$title',$Product_id,$spec,$price,'$content','$Home_t',$hits,$user,now(),'$picName',$ppt,$fid)";
		
		$result=mysql_query($sql);
		if($result)
		{
			echo "<script>alert('产品产品成功！');location.href='product.php'</script>";
		}
		else
		{
			echo "<script>alert('产品添加失败！');location.href='p_add.php'</script>";	
		}

}
			


?>