Php 将CSV数据导入MySQL数据库中的字段

Php 将CSV数据导入MySQL数据库中的字段,php,html,mysql,csv,Php,Html,Mysql,Csv,我正在尝试通过web表单上传一个CSV文件,其中包含以下链接中所述的字段[参见CSV示例]。用户可以通过单击上载按钮浏览其计算机,选择其CSV文件,然后单击上载,然后将CSV数据导入相应字段中的数据库。当用户上载其CSV文件时,该行为空,不包含CSV文件包含的任何数据。有没有办法解决这个问题,以便将CSV文件中的数据导入数据库并放置在其关联字段中 function csv_to_array($filename='', $delimiter=',') { if(!file_exists($

我正在尝试通过web表单上传一个CSV文件,其中包含以下链接中所述的字段[参见CSV示例]。用户可以通过单击上载按钮浏览其计算机,选择其CSV文件,然后单击上载,然后将CSV数据导入相应字段中的数据库。当用户上载其CSV文件时,该行为空,不包含CSV文件包含的任何数据。有没有办法解决这个问题,以便将CSV文件中的数据导入数据库并放置在其关联字段中

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

   $header = NULL;
   $data = array();
   if (($handle = fopen($filename, 'r')) !== FALSE)
   {
     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
     {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
     }
     fclose($handle);
   }
   return $data;
}



$filedata= csv_to_array($_FILES  ['uploaded_file']['tmp_name']);

foreach($filedata as $data)
{
    $sql = "INSERT into table SET value1='".$data['value1']."'......";
}
CSV示例:

uploads.php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

   $header = NULL;
   $data = array();
   if (($handle = fopen($filename, 'r')) !== FALSE)
   {
     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
     {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
     }
     fclose($handle);
   }
   return $data;
}



$filedata= csv_to_array($_FILES  ['uploaded_file']['tmp_name']);

foreach($filedata as $data)
{
    $sql = "INSERT into table SET value1='".$data['value1']."'......";
}
上传_file.php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

   $header = NULL;
   $data = array();
   if (($handle = fopen($filename, 'r')) !== FALSE)
   {
     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
     {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
     }
     fclose($handle);
   }
   return $data;
}



$filedata= csv_to_array($_FILES  ['uploaded_file']['tmp_name']);

foreach($filedata as $data)
{
    $sql = "INSERT into table SET value1='".$data['value1']."'......";
}
试试这个


要保存在csv文件中的分隔符中的第三个参数为fgetcsv。例如:逗号、分号等。

请提供更多说明,以便插入csv数据或csv文件。请记住,您是在双引号字符串中插入$date之类的内容。该字符串将被解析,变量将被替换,如果不存在这样的变量,php将在严格模式下抱怨我这样说是因为我没有看到声明的变量。有没有办法在查询中使用位置参数而不是变量名?喜欢值?、?、?、…,可以将数组作为参数传递,位置参数将从array@LuisMasuelli我还在学习PHP的诀窍,你所说的位置参数是什么意思?请看这个例子:-你可以称之为$dbLink->prepare。然后,像示例中那样绑定参数。最后,您必须执行查询。尝试更改我希望这是有用的。我已尝试使用您的代码。无论CSV文件中有多少行,它都会添加,但字段仍然为空@mehul它们不是相同的东西吗?:S@Mehul这是我上传文件@Mehul后数据库的外观抱歉,我在插入查询变量$row中有一个错误a不确定更改变量$data
function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

   $header = NULL;
   $data = array();
   if (($handle = fopen($filename, 'r')) !== FALSE)
   {
     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
     {
        if(!$header)
            $header = $row;
        else
            $data[] = array_combine($header, $row);
     }
     fclose($handle);
   }
   return $data;
}



$filedata= csv_to_array($_FILES  ['uploaded_file']['tmp_name']);

foreach($filedata as $data)
{
    $sql = "INSERT into table SET value1='".$data['value1']."'......";
}