1

I am working on custom plugin. I need to insert currency names and symbols in table on plugin activation. Right now my database insertion code is :

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

Everthing is working fine except this database insertion on plugin activation. Have you guys any idea. How to do it?

This code gave me error

The plugin generated 416 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

I want to add Currencies name & symbols these all.

  • can you confirm the "wp_woo_currency" table was created or not? – Renish Khunt Mar 20 '17 at 6:56
  • yeah that is created on plugin activation. – Mukii kumar Mar 20 '17 at 6:57
1

You may please use this code that will help you to insert data into database

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

}

Hope this will help you to achieve your goal.

  • 1
    i mentioned above all things are working fine. Just clear me how to insert a large quantity of values statically on plugin activation – Mukii kumar Mar 20 '17 at 7:02
  • 1
    You can add the values in the database using Query which will be inserted inside the aboce function. Since you want it to be inserted Statically this will be much easier for you. Do you mean you want to insert all the currency as well as the symbols for it in the DB? – Pratik bhatt Mar 20 '17 at 7:04
  • 1
    yes @Pratik i want all those values. – Mukii kumar Mar 20 '17 at 7:06
  • 1
    Give me some time I will make a code for you – Pratik bhatt Mar 20 '17 at 7:08
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.