WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know this type of question has been asked over and over, but I couldn't find a solution for my problem, so I hope you can help me. I am using WP 3.3. and I have created a custom table. Now I want to insert some data into it, but I can't get it working. This is my code:

global $wpdb;
$table_name = $wpdb->prefix . "my_data";
$wpdb->insert($table_name, array(
            'my_id'     => NULL,
        'my_day'    => $day,
        'my_month'  => $month,
        'my_abbr'   => $abbr,
        'my_venue'  => $venue,
        'my_geo'    => $geo_one.", ".$geo_two,
        'my_artist' => $artist,
        'my_link'   => $link

I am trying to insert data for several hours now without luck. To my eyes the code is correct, but I guess I am just mussing something important here. Any pointers would be appreciated! Thank you

share|improve this question
Use $wpdb->print_error(); in the next line to insert and check if it throws any errors. – tamilsweet Dec 27 '11 at 5:07
Many thanks. $wpdb->show_errors() did not show anything but print_error() pointed exactly what the problem was! – Valentin Despa Nov 2 '12 at 10:02
up vote 7 down vote accepted

When $wpdb method doesn't perform as it should it is likely issue with resulting SQL query (because of wrong input or something else).

Follow wpdb reference in Codex for troubleshooting:

  • enable database error display via $wpdb->show_errors()
  • check what query is being formed and run via $wpdb->last_query
share|improve this answer

The wpdb-class has not an insert; you shouldt use the query; see the codex and you dont find this function insert() - http://codex.wordpress.org/Class_Reference/wpdb

An example to insert a custom table on WPDB; use an default CREATE TABLE-SQL Syntax and create with the query() of wpdb the table with ypur values.

        $table = $GLOBALS['wpdb'] -> prefix . 'my_data';
        $GLOBALS['wpdb'] -> query(
            "CREATE TABLE $table (
            called_by varchar(96) NOT NULL,
            my_name varchar(96) NOT NULL,
            my_type varchar(15) NOT NULL,
            first_call int(11) NOT NULL,
            arg_count tinyint(4) NOT NULL,
            file_name varchar(128) NOT NULL,
            line_num smallint NOT NULL,
            PRIMARY KEY (first_call, my_name) )"

To insert this source use the register_activation_hook()-hook; only on activate the plugin was install this table, if not exists. On register_uninstall_hook() I delete custom tables.

share|improve this answer
wpdb does have insert() method – Rarst Jan 3 '12 at 19:24
yes, i write this on the comment. – bueltge Jan 8 '12 at 18:59

Your Answer


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.