2016-10-05 114 views
1

我想用excel表格将数据导入到mysql表格中,但是我得到这些错误有人请让我从这里出来..请看看php代码只忽略html的东西。将excel表格导入到mysql表格中用php

<?php 
    include ("connection.php"); 

    if(isset($_POST["submit"])) 
    { 
     $file = $_FILES['file']['tmp_name']; 
     $handle = fopen($file, "r"); 
     $c = 0; 
     while(($filesop = fgetcsv($handle, 1000, ",")) !== false) 
     { 
      $name = $filesop[1]; 
      $email = $filesop[2]; 

      $sql = mysql_query("INSERT INTO co (name, email) VALUES ('$name','$email')"); 

      $c = $c + 1; 

     } 
     fcose($file); 

      if($sql){ 
       echo "You database has imported successfully. You have inserted ". $c ." recoreds"; 
      }else{ 
       echo "Sorry! There is some problem."; 
      } 

    } 
?> 

this is excel sheet

in database it is showing different format

+1

它看起来并不像您的文件以CSV格式保存 - 您不能使用fgetcsv来读取Excel格式的文件。 – iainn

+0

要导入excel文件到数据库你必须使用一些像http://phpexcel.codeplex.com/插件,否则在这里你必须保存你的excel文件到csv并使用此代码导入,它将工作 – Anju

+0

似乎你的文件是sample1 .csv,这确实是正确的格式。现在,你在数据库中发现了无用的字符。在开始之前尝试将字符集设置为utf8。为此,在所有事情之前运行这个查询 - SET NAMES utf8。另外,确保你的表,数据库的字符集是utf8 – vishwakarma09

回答

0

有图书馆excellibrary/php-excel-reader/excel_reader2.phpexcellibrary/SpreadsheetReader.php

您可以使用这些库来阅读你的内容并把它转换成数组。 将电子表格数据转换为数组后,您可以使用迭代轻松地插入数据库。