<?php 
 mysql_connect("localhost", "compaint_paint", "Nlqg*%QatI!!");
//mysqli_connect("localhost", "root", "", "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['ledger_id']=$data[0];
				$dataArray['c_name']=$data[1];
				$dataArray['pre_address']=$data[2];
				$dataArray['tinno']=$data[3];
				$dataArray['eccno']=$data[4];
				 
				
				
			    RecordInsert('csv_client_master_new', $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>