I am trying to insert data to a custom table using plugin activation hook register_activation_hook
so 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' );