导入CSV数据时跳过重复值

时间:2014-10-01 07:13:11

标签: php mysql csv

我的 csv 文件包含 2 列(公司名称,客户名称)。我在 tbl_company 中插入company_name,在 tbl_customer 中插入customer_name和company_id。所以company_id是tbl_customer中的外键。现在我面临的问题是我不想在我的tbl_company中插入公司名称超过1次。任何人都可以检查我写这个条件的代码。

我的PHP代码

$filename=$_FILES["file"]["tmp_name"];
    if($_FILES["file"]["size"] > 0)
    {
        $file = fopen($filename, "r");
        $x = 0;
        while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
        {
                    ini_set('max_execution_time', 300);
                    if($x > 0) {
                    // Insert company name into company table
                    $sql_comp = "INSERT into tbl_company(company_name) values ('$emapData[0]')";
                    mysql_query($sql_comp);
                    //$lastID = mysql_insert_id();
                    // get last ID from tbl_company and insert as foreign key in tbl_customer
                    $sql_LID = "SELECT * FROM tbl_company ORDER BY id DESC";
                    $res_LID = mysql_query($sql_LID);
                    $row_LID = mysql_fetch_array($res_LID);
                    $lastID = $row_LID['id'];
                    // insert company id and customer name into table customer
                    $sql_cust = "INSERT into tbl_customer(comp_id,customer_name) values ('$lastID', '$emapData[1]')";
                    mysql_query($sql_cust);
                    }
                    $x++;
        }
        fclose($file);
        echo 'CSV File has been successfully Imported';
        header('Location: index.php');
    }

2 个答案:

答案 0 :(得分:2)

这可能不是最好的方法,但你可以设计这样的东西:

$filename = $_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0) {
    $file = fopen($filename, "r");
    ini_set('max_execution_time', 300);

    while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {

        $company_name = $emapData[0];

        // checking
        // get ID from tbl_company and insert as foreign key in tbl_customer
        $sql_LID = "SELECT * FROM tbl_company WHERE company_name = '$company_name'";
        $res_LID = mysql_query($sql_LID);


        if(mysql_num_rows($res_LID) <= 0) {
            // if does not exist
            // Insert company name into company table
            $sql_comp = "INSERT into tbl_company(company_name) values ('$company_name')";
            mysql_query($sql_comp);   
            $lastID = mysql_insert_id();

        } else {
            $row_LID = mysql_fetch_array($res_LID);
            $lastID = $row_LID['id'];
        }


        // insert company id and customer name into table customer
        $sql_cust = "INSERT into tbl_customer(comp_id,customer_name) values ('$lastID', '$emapData[1]')";
        mysql_query($sql_cust);


    }
    fclose($file);
    echo 'CSV File has been successfully Imported';
    header('Location: index.php');
}

答案 1 :(得分:0)

使用以下行将CSV文件映射到数组:

$array = array_map('str_getcsv', file('filename.csv'));

然后,您可以迭代该数组,并在foreach循环中向数据库中插入一行。

例如:

foreach($array as $key => $val) {
    $update = $db->prepare('UPDATE table SET value = ? WHERE key = ?');
    $update->execute(array($val, $key));

}

要跳过重复的记录,在进入循环之前创建一个空数组,并在循环内部添加一个条件,以检查迭代的值是否在单独的数组中,如下所示:

$records = [];

//inside of foreach loop

if(!in_array($value, $records)) {
    //insert record
    $records[] = $value;
}

另外,请尽量避免使用mysql_ *函数,因为它们已被弃用,并且不是最适合与数据库通信的。