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.

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

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler