PHP

Import Excel file data in mysql database using PHP

This file is just use to upload excel sheet into any specific location on server.

<?php
 
 ini_set('upload_max_filesize', '512M');
 ini_set('post_max_size', '128M'); 
 
 $hostname = "localhost";
 $username = "root";
 $password = "";
 $database = "read_excel_db";
 
 $conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
 mysql_select_db("$database", $conn) or die(mysql_error());
 
 
 if(!empty($_POST['submit']))
 {
 mysql_query("TRUNCATE excel1");
 $temp=true;
 $input_array = array_map('trim',$_POST); 
 if($temp)
 {
 if (move_uploaded_file($_FILES['file']['tmp_name'], "upload_csv1".'/'.$_FILES['file']['name'])) {
 $file = fopen("upload_csv1".'/'.$_FILES['file']['name'],"r");
 $i=0;
 while(!feof($file))
 {
 $line = fgetcsv($file);
 echo "<pre>"; print_r($line);
 if(!empty($line[0]) && $line[1]!='id')
 {
 $sql = "insert into excel1 set id='".$line[1]."', image='".$line[16]."', no_image='".$line[15]."', miscall='".$line[19]."' ";
 mysql_query($sql);
 }
 $i++;
 }
 die;
 fclose($file);
 } 
 }
 echo "<script>window.location.href='second-sheet.php'</script>";
 exit; 
 }
 ?>

Now we create a html form to upload .csv file. Above code read CSV file fields value and insert them into mysql table.

Import Excel file data in mysql database using PHP

 

<form method="post" enctype="multipart/form-data">
 <input type="file" name="file" />
 <input type="submit" name="submit" value="Submit" />
</form>

Thank You, you have done it.

Being Idea

Being Idea is a web platform of programming tutorials to make better programming skills and provides Software Development Solutions.

This website uses cookies.