这是我使用php导入csv文件的代码。我可以了解错误。请离开它......
<?php
require_once 'database.php';
$database=new Database;
$database->connect();
if (!empty($_FILES)) {
$file = $_FILES["file1"]["tmp_name"];
$handle = fopen($file,"r");
while ($data = fgetcsv($handle,10000, ","))
{
$query = "INSERT INTO personalinfo( id,name, age, gender, occupation, contact, email, uname, paswrd, created_date ) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
NOW()
)
";
$database->insert($query);
echo "INSERT INTO personalinfo( id,name, age, gender, occupation, contact, email, uname, paswrd, created_date ) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."',
'".addslashes($data[3])."',
'".addslashes($data[4])."',
'".addslashes($data[5])."',
'".addslashes($data[6])."',
'".addslashes($data[7])."',
'".addslashes($data[8])."',
NOW()
)";
echo "<br />";
echo("Data inserted");
fclose($file);
}
}
?>
错误是这样的: -
注意:未定义的索引:第10行/var/www/trainees/B9/Jinal/mvc/import.php中的file1
警告:fopen()[function.fopen]:第11行的/var/www/trainees/B9/Jinal/mvc/import.php中的文件名不能为空
警告:fgetcsv()期望参数1为资源,第12行的/var/www/trainees/B9/Jinal/mvc/import.php中给出布尔值
答案 0 :(得分:0)
这是一个简单的例子,但你需要适应你的需要
set_time_limit(10000);
$con = mysql_connect('127.0.0.1','root','password');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$fp = fopen("file.csv", "r");
while( !feof($fp) ) {
if( !$line = fgetcsv($fp, 1000, ';', '"')) {
continue;
}
$importSQL = "INSERT INTO table_name VALUES('".$line[0]."','".$line[1]."','".$line[2]."')";
mysql_query($importSQL) or die(mysql_error());
}
fclose($fp);
mysql_close($con);