Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将CSV数据导入mySQL,第2部分_Php_Mysql_Csv - Fatal编程技术网

使用PHP将CSV数据导入mySQL,第2部分

使用PHP将CSV数据导入mySQL,第2部分,php,mysql,csv,Php,Mysql,Csv,继续我的最后一个帖子。尝试通过PHP上载脚本将用户生成的CSV导入MySQL。上载成功,但由于权限问题,我无法使用加载数据。以下是我想做的: $row = 1; if (($handle = fopen($target_path, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p>

继续我的最后一个帖子。尝试通过PHP上载脚本将用户生成的CSV导入MySQL。上载成功,但由于权限问题,我无法使用加载数据。以下是我想做的:

$row = 1;
if (($handle = fopen($target_path, "r")) !== FALSE) 
  {
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
     $num = count($data);
     echo "<p> $num fields in line $row: <br /></p>\n";
     $row++;
 for ($c=0; $c < $num; $c++) 
 {
  $fullRow = $fullRow . $data[$c] . "," ;
 }
 echo $fullRow;
mysql_query("INSERT INTO foo (field1, field2, field3, field4) VALUES ('$fullRow')");
$fullRow = NULL;
}
   fclose($handle);
  }
$row=1;
if($handle=fopen($target_path,“r”)!==FALSE)
{
while(($data=fgetcsv($handle,1000,“,”)!==FALSE)
{
$num=计数($data);
echo“$num字段位于第$row行:

\n”; $row++; 对于($c=0;$c<$num;$c++) { $fullRow=$fullRow.$data[$c]。“,”; } echo$fullRow; mysql_查询(“插入foo(field1、field2、field3、field4)值(“$fullRow”)”; $fullRow=NULL; } fclose($handle); }

echo$fullRow
从CSV文件中逐字打印出该行的副本,但结尾处有一个额外的逗号。这就是插件无法正常工作的原因吗?当我通过phpMyAdmin手动上传时,CSV文件导入时不会出现问题。或者代码的
值('$fullRow')
位有问题吗?

您只需删除最后一个逗号即可

for ($c=0; $c < $num; $c++) 
 {
  $fullRow = $fullRow . $data[$c] . "," ;
 }
 echo $fullRow;
 $fullRow = substr($fullRow,0,-1);

Paolo_NL_FR的修复程序应该可以让你启动并运行。不过,该脚本可以使用一些TLC,甚至没有基本的sql注入保护。也许可以试试这样:

if (($handle = fopen($target_path, "r")) !== FALSE) 
{
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $values = implode(",", array_map('mysql_real_escape_string', $data));
        mysql_query("INSERT INTO foo (field1, field2, field3, field4) VALUES ($values);");
    }

    fclose($handle);
}
if (($handle = fopen($target_path, "r")) !== FALSE) 
{
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $values = implode(",", array_map('mysql_real_escape_string', $data));
        mysql_query("INSERT INTO foo (field1, field2, field3, field4) VALUES ($values);");
    }

    fclose($handle);
}