dt1888 2018-07-03 07:32
浏览 140
已采纳

WordPress将文本文件中的数据插入到自定义表中

I am trying to insert data to a custom table using plugin activation hook register_activation_hookso when the plugin active the database and its data will insert automatically. The data is in a text file in plugin directory. When I active the plugin database created but no the insert. I thing I did mistake to read the txt file. can someone help me to solve this?

I have a list of data into a text file format like

123|Jhon Doe
124|Michel Muller
125|Jems Curter
126|Miss Weedy
127|Burgel Heigen

I am trying to import them into a wordpress database but failed.

bellow is my code.

//creating db table 
function sbl_employee_create_db() {

    global $wpdb;

    $charset_collate = $wpdb->get_charset_collate();
    $table_employee  = $wpdb->prefix . 'sbl_employee';
    $table_br_name   = $wpdb->prefix . 'sbl_br_name';



    $sql = "CREATE TABLE IF NOT EXISTS $table_br_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        br_code int(5) NOT NULL,
        br_name varchar(220) NOT NULL,
        UNIQUE KEY id (id)
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}


//function to insert branch data to database
function insert_brcode_name(){
    global $wpdb;
    $textCnt   = plugin_dir_path( __FILE__ )."data.txt";
    $file = fopen($textCnt, 'r');

    $arrfields = explode('|', $file);

    $br_code =  $arrfields[0]; 
    $br_name =  $arrfields[1]; 


    $data = array(
                    'br_code'           => $br_code,
                    'br_name'           => $br_name,
                   );

        //format values: %s as string; %d as integer
        $format = array(
            '%s', '%d'
          );
        $wpdb->insert( $tablename, $data, $format );

}

//do action when plugin active
register_activation_hook( __FILE__, 'sbl_employee_create_db' );
register_activation_hook( __FILE__, 'insert_brcode_name' );
  • 写回答

1条回答 默认 最新

  • douweiluo0600 2018-07-03 13:49
    关注

    There are several issues with the insert_brcode_name() function:

    • Assuming the file was successfully opened (via fopen()), $file is actually a file pointer resource and not a string. So $arrfields = explode('|', $file); won't work — it won't give you the result that you expected (and PHP will throw a warning).

    • Secondly, the $format should be array( '%d', '%s' ) and not array( '%s', '%d' ), because in the $data array, the first item is br_code (integer/int) and the second item is br_name (string/varchar). So the first item in $format is for the first item in $data; the second item in $format is for the second item in $data; and so on for other items.

    • $tablename is not defined; which I believe is the $wpdb->prefix . 'sbl_br_name' table.

    So here's the insert_brcode_name() without the above issues: (tried and tested working)

    function insert_brcode_name(){
        $textCnt = plugin_dir_path( __FILE__ ) . "data.txt";
        $file = @fopen($textCnt, 'r');
    
        // Make sure that it's a valid file pointer resource.
        if ( ! $file ) {
            return false;
        }
    
        global $wpdb;
        $tablename = $wpdb->prefix . 'sbl_br_name';
    
        // Reads each line in the file.
        while ( ! feof( $file ) ) {
            $line = fgets( $file );
            $arrfields = explode('|', $line);
    
            // Ignores invalid entries..
            if ( count( $arrfields ) < 2 ) {
                continue;
            }
    
            $br_code = $arrfields[0];
            $br_name = $arrfields[1];
            //echo $br_code . '|' . $br_name . '<br>';
    
            $data = array(
                'br_code' => $br_code,
                'br_name' => $br_name,
            );
    
            //format values: %s as string; %d as integer
            $format = array(
                '%d', // Format of `br_code`
                '%s', // Format of `br_name`
            );
    
            $wpdb->insert( $tablename, $data, $format );
        }
    
        fclose( $file );
    }
    

    Note: The code was re-indented for clarity. There were also minor changes, in addition to the major fixes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,