Products
GG网络技术分享 2025-03-18 16:12 8
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 Doe124|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\' );
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我正在尝试使用插件激活挂钩 register_activation_hook </ code>将数据插入到自定义表中,所以当 插件激活数据库,其数据将自动插入。 数据位于插件目录中的文本文件中。 当我激活创建的插件数据库但没有插入时。 我错误地读取了txt文件。 有人可以帮我解决这个问题吗?</ p>
我有一个文本文件格式的数据列表,如</ p>
123 | Jhon Doe \\ n124 | Michel Muller 125 | Jems Curter
126 | Miss Weedy
127 | Burgel Heigen
</ code> </ pre>
我正在尝试将它们导入wordpress数据库但失败了。 </ p>
下面是我的代码。</ p>
//创建db table function sbl_employee_create_db(){
global $ wpdb; \\ n
$ charset_collate = $ wpdb-&gt; get_charset_collate();
$ table_employee = $ wpdb-&gt;前缀。 \'sbl_employee\';
$ table_br_name = $ wpdb-&gt;前缀。 \'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);
//用于将分支数据插入数据库
函数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\'=&gt; $ br_code,
\'br_name\'=&gt; $ br_name,
);
//格式值:%s为字符串 ; %d为整数
$ format = array(
\'%s\',\'%d\'
);
$ wpdb-&gt; insert($ tablename,$ data,$ format);
}
//在插件激活时执行操作
register_activation_hook(__ FILE __,\'sbl_employee_create_db\');
register_activation_hook(__ FILE __,\'insert_brcode_name\');
</ code> </ pre>
</ div>
网友观点:
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.
Demand feedback