【发布时间】:2016-10-05 09:16:30
【问题描述】:
我想用 php 将 excel 工作表数据导入 mysql 表,但我遇到了这些错误,有人请让我摆脱这种情况。请查看 php 代码,只忽略 html 内容。
<?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.";
}
}
?>
【问题讨论】:
-
您的文件似乎不是以 CSV 格式保存的 - 您不能使用
fgetcsv读取 Excel 格式的文件。 -
要将excel文件导入数据库,您必须使用phpexcel.codeplex.com之类的插件,否则您必须将您的excel文件保存到csv并使用此代码导入,它会起作用
-
您的文件似乎是 sample1.csv,这确实是正确的格式。现在,您在数据库中得到了乱七八糟的字符。在开始之前尝试将字符集设置为 utf8。为此,在所有内容之前运行此查询 - SET NAMES utf8。另外,请确保您的表的字符集,数据库是 utf8