Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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 Jan 21 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. –  mattnewbie Jan 21 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 Jan 21 at 17:50
add comment

1 Answer 1

up vote 2 down vote accepted
+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.

share|improve this answer
    
Profits also has an s on the end –  mattnewbie Jan 22 at 21:07
1  
I edited my answer. –  Scuba Kay Jan 22 at 21:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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