Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip duplicate values when importing CSV data

Tags:

php

mysql

csv

I have a csv file with 2 columns (Company Name, Customer Name). I insert company_name in tbl_company and customer_name and company_id in tbl_customer. So company_id is the foreign key in tbl_customer. Now the problem I am facing is that I dont want to insert company name more than 1 time in my tbl_company. Can anyone please check the code where I write this condition.

My PHP code

$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');
    }
like image 596
Shams Avatar asked Jan 29 '26 03:01

Shams


1 Answers

This may not be the best way, but you could do devise something like this:

$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');
}
like image 94
Kevin Avatar answered Jan 31 '26 15:01

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!