<?php 
mysqli_connect("localhost", "compaint_paint", "Nlqg*%QatI!!", "compaint_paint"); //your database
 function GetFieldValue($table,$condition ,$field)
{
    	 $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")
     {
	     // $q=mysqli_query($link, "truncate table csv_client_master");
     
         $filename = $_FILES['sel_file']['tmp_name'];
         $handle = fopen($filename, "r");
    
	

         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
         {
  		$dataArray['pre_country']=$data[0];
		$dataArray['pre_state']=$data[1];
		$dataArray['pre_city']=$data[2];
 		$dataArray['pre_pincode']=$data[3];
		$dataArray['c_name']=$data[4];
			$dataArray['short_name']=$data[5];
 		$dataArray['ledger_id']=$data[6];
		$dataArray['salesman_id']=$data[7];
		$dataArray['ccategory']=$data[8];
		
			$dataArray['pre_address1']=strip_tags($data[9]);
			
		$dataArray['pre_mobile']=$data[10];
	
		$dataArray['con_person_1']=$data[11];
		$dataArray['con_per_des_1']=$data[12];
		$dataArray['con_person_2']=$data[13];
		$dataArray['con_per_des_2']=$data[14];
		
		$dataArray['per_email']=$data[15];
		
		
		$dataArray['lst_date']=$data[16];
		
		$dataArray['pf_date']=$data[17];
		$dataArray['cst_no']=$data[18];
	 
		
		$dataArray['excies_no']=$data[19];
		$dataArray['excies_date']=$data[20];
		
		$dataArray['company_req_date']=$data[21];
		$dataArray['termination_date']=$data[22];
		$dataArray['pan_no']=$data[23];
		$dataArray['Extension']=$data[24];
		$dataArray['pre_phone1']=$data[25];
		$dataArray['pre_phone2']=$data[26];
				 
				
				
			    RecordInsert('csv_client_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>