2013-05-08 53 views
0

用戶只能上傳.tsv格式files.and我要將此文件導入具有獨特value.duplicates MySQL的不allowed.how來幹這事嘗試這種代碼,但它給插入數據時出錯。進口.tsv格式文件轉換成具有獨特價值的MySQL

<?php 
    include_once("class/dbo.class.php"); 

$message = null; 

$allowed_extensions = array('tsv'); 

$upload_path = 'uploads'; 

if (!empty($_FILES['file'])) { 

    if ($_FILES['file']['error'] == 0) { 

     // check extension 
     $file = explode(".", $_FILES['file']['name']); 
     $extension = array_pop($file); 

     if (in_array($extension, $allowed_extensions)) { 

      if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) 
      { 

       $filename = $upload_path.'/'.$_FILES['file']['name']; 

       $content = file_get_contents($filename); 

       $lines = explode("\n", $content); 

       $columns = explode("\t", $lines[0]); 
       //print_r($columns); 

       $sql_insert = "\n\nINSERT INTO tsv_table ("; 
       foreach ($columns as $column) 
       { 
        if($column == end($columns)) 
         $sql_insert .= "'".addslashes($column)."')"; 
        else 
         $sql_insert .= "'".addslashes($column)."', "; 
       } 
       $sql_insert .= " VALUES\n"; 

       $total_lines = count($lines) -1; 

       for($i=1;$i<$total_lines;$i++) 
       { 

        $fields = explode("\t", $lines[$i]); 
        //print_r($fields); 
        //exit; 
        $sql_insert .= "("; 
        foreach ($fields as $field) 
        { 
         if($field == end($fields)) 
          $sql_insert .= "'".addslashes($field)."'"; 
         //else if($field == null) 
          //$sql_insert .= "'',"; 
         else 
          $sql_insert .= "'".addslashes($field)."', "; 
        } 
        if(($i+1) == $total_lines) 
         $sql_insert .= ");"; 

        else 
         $sql_insert .= "),\n"; 
       } 

        $d=new dbo(); 
        dbo.$d->dml($sql_insert); 
        echo "Data imported successfully."; 
      } 

     } else { 
      $message = '<span class="red">Only .tsv file format is allowed</span>'; 
     } 

    } else { 
     $message = '<span class="red">There was a problem with your file</span>'; 
    } 

} 

?> 
<html> 
<head> 
    <title>Upload TSV to MySQL</title> 
    <link href="css/core.css" rel="stylesheet" type="text/css" /> 
</head> 
<body> 

<section id="wrapper"> 

    <form action="" method="post" enctype="multipart/form-data"> 

     <table cellpadding="0" cellspacing="0" border="0" class="table"> 
      <tr> 
       <th><label for="file">Select file</label> <?php echo $message; ?></th> 
      </tr> 
      <tr> 
       <td><input type="file" name="file" id="file" size="30" /></td> 
      </tr> 
      <tr> 
       <td><input type="submit" id="btn" class="fl_l" value="Submit" /></td> 
      </tr> 
     </table> 

    </form> 

</section> 

</body> 
</html> 

錯誤: 您的SQL語法錯誤;檢查對應於你的MySQL服務器版本在線路附近使用'ID」,‘LAST_SCAN_DATE’,‘名’,‘MONSTER_CLASS’,‘POWER_LEVEL’,‘培訓師’,‘邁樂’正確的語法手冊1

.tsv文件包含一些具有空值的字段。

回答

0

不要引用表列。

嘗試

$sql_insert = "\n\nINSERT INTO tsv_table ("; 
foreach ($columns as $column) 
{ 
    if($column == end($columns)) 
    $sql_insert .= addslashes($column).")"; 
    else 
    $sql_insert .= addslashes($column)." "; 
} 
+0

仍無法正常工作:列數並不在行1 – 2013-05-08 06:59:19

+0

匹配值計數它打印插入查詢是這樣的:INSERT INTO tsv_table(ID,LAST_SCAN_DATE,NAME,MONSTER_CLASS,POWER_LEVEL,教練, MELEE_ATTACK DEF_CRUSHING DEF_SLASHING DEF_PIERCING DEF_FIRE DEF_COLD DEF_ELEC DEF_WIND DEF_VOID DEBUFFS_AFFECTED值('1','2013-05-04T18:34:43.136Z', '平常','平均','平均','弱','弱','平均','抗性','抗性','平均','平均」, '平均', ''),( '2', '2013-05-04T18:35:41.138Z', '觸手怪物', 'COMMON', '' '' '刺穿',..... ... – 2013-05-08 07:00:24

+0

我注意到POWER_LEVEL和TRAINER空值沒有用逗號分隔,因此計數不匹配。如果你取消註釋否則,如果($場== NULL)語句 – Chief 2013-05-08 07:09:37