如何将CSV上传到wordpress自定义表

时间:2016-06-06 08:58:57

标签: php mysql wordpress csv

如何将CSV数据上传到wordpress自定义表wp_sonali_data。贝娄是我正在尝试的代码。但没有运气请帮助

这是我的自定义表格列

id
brcode
brname
dist

这是我的上传者

   <form method="post" action="">
<p>Choose your CSV file</p><br />
<input type="file" name="file" />
<input type="submit" name="submit" value="submit"/>

</form>


<?php

    //Upload CSV File
    if (isset($_POST['submit'])) {

    global $wordpress,$wpdb;
        $datafile= $_FILES['file']['tmp_name'];
        $file=$upload_dir['basedir'].'/'.$_FILES['file']['name'];
        $fileurl=$upload_dir['baseurl'].'/'.$_FILES['file']['name'];
        if (!move_uploaded_file($_FILES['file']['tmp_name'],$file)){
            print_r('Failed to move uploaded file.');
            }
        $sql="
        LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE wp_sonali_data
        FIELDS TERMINATED BY ',' 
        LINES TERMINATED BY '\r\n'
        (id,brcode,brname,dist);
        ";
        $query = $wpdb->query($sql);
}

错误

Output: 'Failed to move uploaded file.'

1 个答案:

答案 0 :(得分:3)

LOAD DATA INFILE "$_FILES['upload_csv']['tmp_name']"
INTO TABLE wp_sonali_data
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
("id", "brname", "brcode", "dist")