<?php 
mysqli_connect("localhost", "root", "", "compaint_paint"); //your database
 function GetFieldValue($table,$condition ,$field)
{
echo "select ".$field." from ".$table." where  $condition  " ;
    	 $sql=mysqli_query($link, "select ".$field." from ".$table." where  $condition  " );
	    $r=mysqli_fetch_array($sql);
     	return ($r[$field]);
}
function RecordInsert($table, $dataArray)
{ 
 	 
  	  $sql="Insert into ".$table ." set ";
 		$i=0;
 		foreach($dataArray as $key => $val)
 			{
 			if($i==0)
			$sql.=" ".$key." = '".$val."'";
			else
			$sql.=", ".$key." = '".$val."'";			
			$i++;
			}
 	   echo "<br>*************INSERT***************************************************</br>";
	    echo $sql;
  $r=mysqli_query($link, $sql);
	return ($r); 
} 


if(isset($_POST['submit']))
{
       $fname = $_FILES['sel_file']['name'];
      $chk_ext = explode(".",$fname);
      if(strtolower($chk_ext[1]) == "csv")
     {
     
         $filename = $_FILES['sel_file']['tmp_name'];
         $handle = fopen($filename, "r");
    
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
         {
		  
		   $qCategoryID=mysqli_query($link, "select id from stockmaincategory where  stockmaincategoryid='FG'  ");
	 		$r1=mysqli_fetch_array($qCategoryID);
 			 $CategoryID=$r1['id'];
			  $qSubCategoryID=mysqli_query($link, "select id from stockcategory where  stockmaincategory='".$CategoryID."'  ");
	 			$r2=mysqli_fetch_array($qSubCategoryID);
			 $SubCategoryID=$r2['id'];
			 
 			  
			   $dataArray['CategoryID']=$CategoryID;
			   $dataArray['SubCategoryID']=$SubCategoryID;
			   $dataArray['StockID']=$data[0];
			   $dataArray['item_name']=$data[1];
			    $dataArray['mst_ledger_code']=$data[2];
			   $dataArray['item_desc']=$data[3];
			   //$dataArray['unit']=   GetFieldValue('unitsofmeasure',"lower(unitname)='".strtolower($data[4])."'" ,'unitid');
			  // $dataArray['mrp_rate']=$data[5];

			   
			   
          		  RecordInsert('item_master', $dataArray);
			echo "<br>************************************************************<br>";
         }
         // fclose($handle);
         echo "Successfully Imported";
     }
     else
     {
         echo "Invalid File";
     }    
}
?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post' enctype="multipart/form-data">

    Import File : <input type='file' name='sel_file' size='20'>
    <input type='submit' name='submit' value='submit'>

</form>