2014-07-08 18 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)); 

        }