我正在开发自定义插件。我需要在插件激活表中插入货币名称和符号。现在,我的数据库插入代码是:
global $wpdb;
$table_name1 = $wpdb->prefix . "woo_currency";
$curr_name = array(\'Dollars\', \'Ruble\', \'Riel\');
$curr_symbol = array(\'$\', \'p.\', \'៛\');
$insert = $wpdb->insert($table_name1,
array(
\'name\' => $curr_name,
\'symbol\' => $curr_symbol,
),array(\'%s\', \'%s\'));
除了在插件激活时插入数据库外,一切正常。你们有什么想法吗。怎么做?
此代码给了我错误
该插件在激活期间生成了416个字符的意外输出。如果您注意到“headers ready sent”消息、联合提要问题或其他问题,请尝试停用或删除此插件。
我想添加Currencies name & symbols 这些都是。
SO网友:Pratik bhatt
您可以使用此代码来帮助您将数据插入数据库
register_activation_hook( __FILE__, \'my_plugin_create_db\' );
function my_plugin_create_db() {
global $wpdb;
$table_name1 = $wpdb->prefix . "woo_currency";
$curr_name = array(\'Dollars\', \'Ruble\', \'Riel\');
$curr_symbol = array(\'$\', \'p.\', \'៛\');
$insert = $wpdb->insert($table_name1,
array(
\'name\' => $curr_name,
\'symbol\' => $curr_symbol,
),array(\'%s\', \'%s\'));
}
希望这将帮助你实现你的目标。
SO网友:Mukii kumar
Ok, finally i got my answer. Just put the insert query in foreach statement. Here is the code:
$curr_name = array( \'Dollars\', \'peso\' );
$curr_symbol = array( \'$\', \'ƒ\' );
foreach( $curr_name as $x => $y )
{
$insert = $wpdb->insert(
$table_name1,
array(
\'name\' => $y,
\'symbol\' => $curr_symbol [$x ],
),
array( \'%s\', \'%s\' )
);
}