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' );

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.