2014-10-01 110 views
3

我有一個csv文件與列(公司名稱,客戶名稱)。我在tbl_company中插入company_name,在tbl_customer中插入customer_name和company_id。所以company_id是tbl_customer中的外鍵。現在我面臨的問題是我不想在我的tbl_company中多次插入公司名稱。任何人都可以檢查我寫這個條件的代碼。導入CSV數據時跳過重複值

我的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'); 
    } 
+0

問題可能更適合http://codereview.stackexchange.com – 2014-10-01 07:16:39

回答

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'); 
} 
+0

我試過了,它爲公司插入工作,但它不會在tbl_customer中插入正確的company_id。如果microsoft在CSV文件中位於第2行和第5行,我想插入Microsoft ID爲1的ID爲1的時間,併爲每個公司爲Microsoft的客戶插入該ID。但是現在它採用最後一個ID,而無需比較CSV文件的客戶和公司名稱。 – Shams 2014-10-01 08:20:54

+0

@Shams ahh好吧你需要那些公司ID,然後檢查我的修訂版,應該能夠得到那些 – Ghost 2014-10-01 08:23:46

+0

是否有公司名稱而不是獲得最後的ID的公司的ID,我認爲這是我的區域可以解決外鍵問題,但我不知道具體到底是什麼? – Shams 2014-10-01 08:26:00

0

地圖的CSV文件使用以下行的數組:

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

然後,您可以遍歷數組的d在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_ *功能,不要因爲他們已經過時,不是最好的獲得與您的數據庫進行通信。