将.tsv文件导入具有唯一值的mysql

时间:2013-05-08 06:34:00

标签: php mysql database import tsv

用户只能上传.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服务器版本相对应的手册,以便在第1行“ID”,“LAST_SCAN_DATE”,“NAME”,“MONSTER_CLASS”,“POWER_LEVEL”,“TRAINER”,“MELE”附近使用正确的语法

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

1 个答案:

答案 0 :(得分: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)." ";
}