<?php
include("db.php");
 if($_POST['submit']){
 
 $customerName=$_POST['customerName'];
  
 $fname=$_POST['fname'];
  
 $id_cart=$_POST['id_cart'];
  
 $telephone=$_POST['telephone'];
  
 $address=$_POST['address'];
  
 $description=$_POST['description'];
  
  //========================================================
   $take_id_sql="select id from havale ORDER BY `id` DESC limit 1";
   $take_id_query= $db ->query($take_id_sql);
   $take_id_row=$take_id_query ->fetch_array();
   $take_id=$take_id_row['id'];
  //========================================================
  
 $image="pic".$take_id;  

 
 if(isset($_FILES['image'])){
		$errors= array();
		$file_name = $_FILES['image']['name'];
		 
		 $explode=explode(".",$file_name);
		 $excatFileName=$explode[0];
		 
		 $file_size =$_FILES['image']['size'];
		 
		$file_tmp =$_FILES['image']['tmp_name'];
		  
		$file_type=$_FILES['image']['type'];
		
		 
		$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
 
		 
		$expensions= array("jpg"); 		
		if(in_array($file_ext,$expensions)=== false){
		
		     
			echo' <script>
					 sweetAlert("هشدار...", "فرمت فایلی که آپلود کردید درست نبود اطلاعات شما ثبت شد لطفا از بخش ویرایش دوباره عکس را آپلود کنید", "error");
			   </script>';
			  $file_name="عکس آپلود نشد"; 
			   
		}
		if($file_size > 2097152){
		$errors[]='File size must be excately 2 MB';
		 
		}				
		if(empty($errors)==true){
			 
			$file_name="pic".$take_id.".jpg";  
			move_uploaded_file($file_tmp,"customerPicthre/".$file_name);
			//echo "Success";
		}else{
			print_r($errors);
		}
	}  
	
	 
	 $insert_sql="insert into havale (customerName,fname,id_cart,telephone,address,description,image) 
	              values
	('$customerName','$fname','$id_cart','$telephone','$address','$description','$image')";
	$insert_query= $db ->query($insert_sql);  

	header("location:havale_Customer.php ");
    echo '<meta http-equiv="refresh" content ="1; url=../havale_Customer.php" >';


}




?>