0

I'm trying to populate a custom table called countries, with two rows of data. The custom table is created when the plugin is activated. The rows need to be added when the plugin is activated. The code for the custom table is fine, & the custom table is created. My code so far:

register_activation_hook( __FILE__, 'mp_insert_countries_rows' );
function mp_insert_countries_rows(){

global $wpdb;
$mp_insert_country = $wpdb->prefix . 'countries';

$insert_country_rows = $wpdb->insert(

$mp_insert_country,
array(
'id' => '1',
'name' => 'UK',
'labour_cost' => '20',
'overheads' => '0.45',
'profit' => '0.22'     
),

array(
'id' => '2',
'name' =>'China',
'labour_cost' => '6',
'overheads' => '0.27',
'profit' => '0.17'  
));  

 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
 dbDelta(  $insert_country_rows );
}

How should I correct this? Thanks

3
  • If you set WP_DEBUG to true in wp-config.php file, what do you notice when you activate the plugin? Also are you really providing table structure for dbDelta function?
    – sri
    Commented Jan 21, 2014 at 15:16
  • @sri The only message I get is on the plugin page, The plugin generated 765 characters of unexpected output during activation. I don't get anything on the actual plugin page . Thanks.
    – user28566
    Commented Jan 21, 2014 at 17:02
  • 2
    I think you are mixing things together. You use dbDelta( $sql ) to create the table and $wpdb->insert( $table, $data, $format ); to insert the data (more here). You don't take the output from $wpdb->insert() into dbDelta(). Check it out and report back.
    – birgire
    Commented Jan 21, 2014 at 17:50

1 Answer 1

1
+50

Looking at this article on WP.org it seems you have to create your database first, with some SQL:

function mp_install_table() {
    global $wpdb;

    $table_name = $wpdb->prefix . "countries";

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        name tinytext NOT NULL,
        labour_cost INT,
        overheads FLOAT,
        profits FLOAT
    );";

   require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
   dbDelta( $sql );
}

And after you have your table you can add rows:

function mp_install_country_data() {
    global $wpdb;
    $table_name = $wpdb->prefix . "countries";

    $wpdb->insert(
        $table_name,
        array(
            'id' => '1',
            'name' => 'UK',
            'labour_cost' => '20',
            'overheads' => '0.45',
            'profits' => '0.22'     
        ),

        array(
            'id' => '2',
            'name' =>'China',
            'labour_cost' => '6',
            'overheads' => '0.27',
            'profits' => '0.17'  
        )
    );  
}

I hope this helps.

2
  • Profits also has an s on the end
    – user28566
    Commented Jan 22, 2014 at 21:07
  • 1
    I edited my answer. Commented Jan 22, 2014 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.