2014-07-08 67 views
-1

我有下面的代碼通過一個CSV文件爆上傳CSV數據更新查詢

 $get_columns = $db_website->prepare("SELECT COLUMN_NAME 
     FROM INFORMATION_SCHEMA.COLUMNS 
     WHERE TABLE_SCHEMA = 'mytable' AND TABLE_NAME = 'products'"); 
     $get_columns->execute(); 

     while ($row = $get_columns->fetch(PDO::FETCH_ASSOC)) { 

      $want[] = $row['COLUMN_NAME']; 

     } 

     $file = fopen($_FILES['filename']['tmp_name'], "r"); 

     $counter = 0; 

     while (!feof($file)) { 

      if ($counter === 1) 
       break; 

      $have = fgetcsv ($file, 5000); 
      ++$counter; 

     } 

     fclose ($file); 

     $map = array_intersect($have, $want); 
     $num_feilds = implode($map); 
     $fields = "`".implode("`,`",$map)."`"; 

     if ($num_feilds != '') { 

      $file = fopen($_FILES['filename']['tmp_name'], "r"); 
      $line = fgetcsv($file, 1000, ","); 
      while (($line = fgetcsv($file)) !== FALSE) { 

       $data = array_intersect_key($line, $map); 

       $implode = str_replace("'", ''', $data); 
       $implode = str_replace("£", '£', $implode); 

       $implode = "'".implode("','",$implode)."'"; 

       $query = $db_website->prepare("SELECT p.stock_id 
       FROM products AS p 
       WHERE p.stock_id = :data"); 
       $query->bindValue(':data', $data[0], PDO::PARAM_INT); 
       $query->execute(); 
       $product_exists = $query->rowCount(); 

       if ($product_exists == 0) { 

        $product_import = "INSERT INTO products ($fields, token, date_created) VALUES ($implode, :token, :date_created)"; 
        $product_import = $db_website->prepare($product_import); 
        $product_import->execute(array(':token'=>$token, ':date_created'=>$todays_date_time)); 

        $update_slug = "UPDATE products SET slug = LOWER(title), 
        slug = replace(slug, char(128), '') 
        WHERE token = :token"; 
        $update_slug = $db_website->prepare($update_slug); 
        $update_slug->execute(array(':token'=>$token)); 

       } else { 

        while ($row = $query->fetch(PDO::FETCH_ASSOC)) { 

         $stock_id = $row['stock_id']; 

         $product_import = "UPDATE products SET $this_is_the_variable_i_need_to_create_from_the_implode, token = :token, date_updated = :date_updated 
         WHERE stock_id = :stock_id"; 
         $product_import = $db_website->prepare($product_import); 
         $product_import->execute(array(':stock_id'=>$stock_id, ':token'=>$token, ':date_updated'=>$todays_date_time)); 

        } 

        $update_slug = "UPDATE products SET slug = LOWER(title), 
        slug = replace(slug, char(128), '') 
        WHERE token = :token"; 
        $update_slug = $db_website->prepare($update_slug); 
        $update_slug->execute(array(':token'=>$token)); 

       } 

      } 

      fclose($file); 

     } 

我的問題在於,我希望它更新現有的產品將記錄插入到數據庫中,並創建新那些。

在上面的代碼中,我已經開始通過查詢來檢查股票ID是否存在,以及它是否不插入帶有else的記錄以說明更新。

我正在努力的部分是如何讓它崩潰COLUMN_NAME以及在csv文件中發送的數據。

任何提示在正確的方向將不勝感激。

謝謝 丹

回答

0

如果我理解你正確,你需要創建一系列基於什麼是$data陣列(這是由單行包含值的陣列組子句您CSV)。不包括任何種類的驗證(無論是在導入文件中的列,或在導入文件中的數據)的,你可以做這樣的事情:

$sets = array(); 
$update_values = array(); 
foreach($data as $index => $val) 
{ 
    if(empty($have[ $index ])) 
    continue; 
    $field_name = $have[ $index ]; 
    $update_values[] = $val; 
    $sets[] = "{$field_name} = ':val{$index}'"; 
} 

if($sets) 
{ 
    $update_values[] = $stock_id; 
    $set_clause = implode(',',$sets); 
    $product_import = $db_website->prepare("UPDATE products SET {$set_clause} WHERE stock_id = :stock_id"); 
    $product_import->execute($update_values); 
} 

同樣,你會想驗證您的輸入,但這應該給你的想法。

+0

非常感謝我得到它的工作,將發佈我的碼 –

0

謝謝oliakaoil,

這是我到底給其他人使用的代碼誰可能需要在未來

    $sets = array(); 
        $update_values = array(); 

        foreach ($data as $index => $val) { 

         if (empty($have[$index])) 
          continue; 
         $field_name = $have[$index]; 
         $update_values[] = $val; 
         $sets[] = "{$field_name} = '{$val}'"; 

        } 

        if ($sets) { 

         $update_values[] = $stock_id; 
         $set_clause = implode(',',$sets); 

         $product_import = "UPDATE products SET {$set_clause}, token = :token 
         WHERE stock_id = :stock_id"; 
         $product_import = $db_website->prepare($product_import); 
         $product_import->execute(array(':stock_id'=>$update_values[0], ':token'=>$token)); 

        }