我用跟踪代码 将CSV输入到我的SQL
if($_POST[ sform ] == "1")
{
$fname = $_FILES[ upload ][ name ];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES[ upload ][ tmp_name ];
$row = 1;
if (($handle = fopen($filename, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$row++;
$data_entries[] = $data ;
}
fclose($handle);
}
foreach($data_entries as $line)
{
$sql = "INSERT into testposts(ID,UID,title,tags,desc) values( , $data[0] , $data[1] , $data[2] , $data[3] )";
$conn->execute($sql);
$conn->execute($line);
}
$msg = "Successfully Imported";
}
else
{
$error = "Invalid File";
}
}
当我尝试更新时, 所有值都会被更新为 0
我的 Csv 文件如下( 例如)
1,title,tags,send2
不包括 PID 值,因为它是自动递增的
所以我试了第二次变换
if($_POST[ sform ] == "1")
{
$fname = $_FILES[ upload ][ name ];
$chk_ext = explode(".",$fname);
if(strtolower($chk_ext[1]) == "csv")
{
$filename = $_FILES[ upload ][ tmp_name ];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$sql = "INSERT into testposts(ID,UID,title,tags,desc) values( , $data[0] , $data[1] , $data[2] , $data[3] )";
mysql_query($sql) or die(mysql_error());
//$conn->execute($sql);
}
$msg = "Successfully Imported";
}
else
{
$error = "Invalid File";
}
}
在我的第二个代码中,它正确地上传了预期的值,但插入了两次。
任何帮助都会感激不尽,谢谢