I have been playing with this for hours and can't seem to make any headway. I've searched but haven't found a solution.
I've created a new table - wp_trackingdata. Every time I try to insert a new row, I find that two new rows have been inserted.
This is my code:
global $wpdb;
$table_name = $wpdb->prefix . "trackingdata";
$sql = $wpdb->prepare(
"INSERT INTO 'wp_trackingdata'
( user_id ) values ( %d )",
2 );
$wpdb->query($sql);
Note that where it has apostrophes around wp_trackingdata in the code above, there are actually backticks in my query. There seem to be a couple of anomalies in this area:
- Only placing backticks round wp_trackingdata in the query seems to work. If I use a straightforward apostrophe or quote mark, no new row gets inserted
- If I replace
wp_trackingdatain the query with $table_name then no new rows get inserted
My table looks like this:
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
site_id bigint(20) NOT NULL,
page_number bigint(20) NOT NULL,
extended_data longtext NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
course_status tinytext NOT NULL,
UNIQUE KEY id (id)
);";
If anyone can see where I'm going wrong, I'd be really grateful to hear.