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
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:$wpdb->show_errors()
$wpdb->last_query
The
wpdb
-class does not have an insert method. It would be best if you didn’t use the query; see the codex, and you don’t find this functioninsert()
– http://codex.wordpress.org/Class_Reference/wpdbAn example of inserting a custom table on WPDB; use a default
CREATE TABLE
-SQL Syntax and create with thequery()
ofwpdb
the table with your values.To insert this source, use the
register_activation_hook()
-hook; only on activation, the plugin will install the table if it doesn’t exist. Onregister_uninstall_hook()
I delete custom tables.