0

I am working on importing .xlsx data to a MySQL database. The data is uploading perfect if I choose a .csv file, but when I choose a .xlsx file it will insert dummy data. How do I solve this? Here is the screenshot :

enter image description here

and here is my code. Found from Google

reference : https://hemant9807.blogspot.in/2016/09/import-excelcsv-file-to-mysql-database.html

<?php 
if(isset($_POST["Import"]))
{
    //First we need to make a connection with the database
    $host=''; // Host Name.
    $db_user= ''; //User Name
    $db_password= '';
    $db= ''; // Database Name.
    $conn=mysqli_connect($host,$db_user,$db_password,$db) or die (mysqli_error());
    //mysql_select_db($db) or die (mysql_error());
    echo $filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        //$sql_data = "SELECT * FROM test";
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
            //print_r($emapData);
            //exit();
            $sql = "INSERT into test(image) values ('$emapData[0]')";
            mysqli_query($conn,$sql);
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
        //header('Location: upload.php');
    }
    else
        echo 'Invalid File:Please Upload CSV File';
}
?> 
Roan
  • 892
  • 15
  • 27
Vinesh Chauhan
  • 1,288
  • 11
  • 27

2 Answers2

0

A CSV file is a plain text file, but an Excel file like xls or xlsx is not. If you want to import directly from an Excel file you need to you a utility that can read the Excel file format.

panofish
  • 7,578
  • 13
  • 55
  • 96
0

Link given by ian0411 is working. i did.nt test it. but i done in this way so this will work.

first i convert my excel file csv and then i uploaded it to server it works fine. Thank you for your answer.

Vinesh Chauhan
  • 1,288
  • 11
  • 27