admin管理员组文章数量:1414621
EDIT: Rewrote the page from scratch and have been unable to figure out the differences. The new page works as expected.
Checked multiple questions here and also on Wordpress with no luck yet. Below is code I'm using in several page to take old records and plug them into the DB (with no issues on those pages). There's more code further in the page but this is where I'm having issues. It's not working on one page.
I'm not getting any errors, even with $wpdb->show_errors(); Only output I'm getting is after I use $wpdb->print_error(), which gives me the output:
`WordPress database error: []
SHOW FULL COLUMNS FROM `wp_posts`
$new_id = $wpdb->insert_id; $new_id at this point is 0 because nothing was inserted into the DB (verified via PHPMyAdmin).
I've tried to indicate what types of data I'm passing in the insert (via third array of '%s', etc) but got the same results.
Any recommendations of other tests or workarounds would be appreciated.
=====================
function transfer_officer($old_record) {
global $wpdb;
$wpdb->show_errors();
$status = 'publish';
$old_id = $old_record[0];
$name = $old_record[1];
$post_date = date('Y-m-d h:i:s');
$post_data = array(
'post_author' => 1,
'post_date' => $post_date,
'post_date_gmt' => $post_date,
'post_content' => '',
'post_title' => $name,
'post_excerpt' => '',
'post_status' => $status,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => officer_name_to_slug($name),
'to_ping' => '',
'pinged' => '',
'post_modified' => $post_date,
'post_modified_gmt' => $post_date,
'post_content_filtered' => '',
'post_parent' => 0,
'menu_order' => 0,
'post_type' => 'rb_board_officers_posts',
'post_mime_type' => '',
'comment_count' => 0
);
//put into post table
$wpdb->insert('wp_posts', $post_data);
$wpdb->print_error();
var_dump($wpdb);
$new_id = $wpdb->insert_id;
EDIT: Rewrote the page from scratch and have been unable to figure out the differences. The new page works as expected.
Checked multiple questions here and also on Wordpress with no luck yet. Below is code I'm using in several page to take old records and plug them into the DB (with no issues on those pages). There's more code further in the page but this is where I'm having issues. It's not working on one page.
I'm not getting any errors, even with $wpdb->show_errors(); Only output I'm getting is after I use $wpdb->print_error(), which gives me the output:
`WordPress database error: []
SHOW FULL COLUMNS FROM `wp_posts`
$new_id = $wpdb->insert_id; $new_id at this point is 0 because nothing was inserted into the DB (verified via PHPMyAdmin).
I've tried to indicate what types of data I'm passing in the insert (via third array of '%s', etc) but got the same results.
Any recommendations of other tests or workarounds would be appreciated.
=====================
function transfer_officer($old_record) {
global $wpdb;
$wpdb->show_errors();
$status = 'publish';
$old_id = $old_record[0];
$name = $old_record[1];
$post_date = date('Y-m-d h:i:s');
$post_data = array(
'post_author' => 1,
'post_date' => $post_date,
'post_date_gmt' => $post_date,
'post_content' => '',
'post_title' => $name,
'post_excerpt' => '',
'post_status' => $status,
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_password' => '',
'post_name' => officer_name_to_slug($name),
'to_ping' => '',
'pinged' => '',
'post_modified' => $post_date,
'post_modified_gmt' => $post_date,
'post_content_filtered' => '',
'post_parent' => 0,
'menu_order' => 0,
'post_type' => 'rb_board_officers_posts',
'post_mime_type' => '',
'comment_count' => 0
);
//put into post table
$wpdb->insert('wp_posts', $post_data);
$wpdb->print_error();
var_dump($wpdb);
$new_id = $wpdb->insert_id;
Share
Improve this question
edited Sep 24, 2015 at 16:38
momn99
asked Sep 23, 2015 at 23:48
momn99momn99
1311 silver badge6 bronze badges
3 Answers
Reset to default 5I got the same problem.
Solution 1: Strip your data.. Wordpress rejects the query if the length of value is greater than the field length defined on the database.
On your insert query, the value for post_type field exceeded the 20 character limit.
Solution 2: Use the $wpdb->query method
Hopefully, you define global variable = $wpdb.
If your $wpdb not working, and also not showing any error then you must try these three steps.
Print your error using with errors functions.
echo $wpdb->last_error;
or
echo $wpdb->show_errors();
If no error visible then you must print your last query using with last query function.
echo $wpdb->last_query();
Copy and paste your last query in your Phpmyadmin > Your Database -> Table -> SQL tab, click on Go button, you will definitely get proper solutions(errors) in it.
Thanks,
Rocky Mehta.
Have you tried using manually entered data to see if that gets inserted? Just make some stuff up to replace the variables. Also check that the post_author has permission to insert posts.
Another option is to try wp_insert_post() and see if that works.
本文标签: wpdbgtinsert not working
版权声明:本文标题:wpdb->insert not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745147080a2644746.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论