最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - Wordpress wpdb->insert returns int(0) => doesn't insert anything, no errors!

programmeradmin3浏览0评论

I am really clueless. I want to do an insertion in my WordPress plugin the problem is it doesn't return any errors nor it insert the stuff!

I don't know how to fix this and really need your help. In the following code i used example names, but i used the character - for these in case of that my real table has also names with - in it as well as the table itself.

Usually there is no problem, just use some back ticks `` and the stuff works well, but now welcome to WordPress. Is there a problem with the insertion function in WordPress or is there any other possible error?

I really tried to fix this by myself but i failed.

$insertion = $wpdb->insert($wpdb->prefix.'table-name-of-plugin', array(
                'column-name' => $stringValueForC1,
                'second-column-name' => $stringValueForC2
            ), array('%s, %s'));

If i use var_dump() for the insertion variable i get: int(0). string 0 for wpdb->last_error and bool(false) for wpdb->last_query.

I also double checked my table name and it is 100% correct! What can be the error?

I am really clueless. I want to do an insertion in my WordPress plugin the problem is it doesn't return any errors nor it insert the stuff!

I don't know how to fix this and really need your help. In the following code i used example names, but i used the character - for these in case of that my real table has also names with - in it as well as the table itself.

Usually there is no problem, just use some back ticks `` and the stuff works well, but now welcome to WordPress. Is there a problem with the insertion function in WordPress or is there any other possible error?

I really tried to fix this by myself but i failed.

$insertion = $wpdb->insert($wpdb->prefix.'table-name-of-plugin', array(
                'column-name' => $stringValueForC1,
                'second-column-name' => $stringValueForC2
            ), array('%s, %s'));

If i use var_dump() for the insertion variable i get: int(0). string 0 for wpdb->last_error and bool(false) for wpdb->last_query.

I also double checked my table name and it is 100% correct! What can be the error?

Share Improve this question edited Mar 14, 2016 at 7:33 Sumit 4,8542 gold badges28 silver badges36 bronze badges asked Mar 13, 2016 at 22:01 user3714751user3714751 1031 silver badge2 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 1

Your format is wrong.

On your code is should be

$insertion = $wpdb->insert($wpdb->prefix . 'table-name-of-plugin', array(
'column-name' => $stringValueForC1,
'second-column-name' => $stringValueForC2
    ), array('%s', '%s') );

Notice the change from

array('%s, %s')

to

array('%s', '%s')

Also, since you are setting the format for both of the value as string. I recommend that you just use

'%s'

The format parameter accepts either array or string. If it's string, the string will be used as the format for all the values. Source - https://codex.wordpress/Class_Reference/wpdb#INSERT_row

Issue exist in the data type format. You've passed array('%s, %s') take a closer look, instead of two values you've passed them as a single value, which leads to incorrect data type format.

It should be array('%s', '%s')

So the final correct statement will look like this

$insertion = $wpdb->insert($wpdb->prefix . 'table-name-of-plugin', array(
    'column-name' => $stringValueForC1,
    'second-column-name' => $stringValueForC2
        ), array('%s', '%s')
);
发布评论

评论列表(0)

  1. 暂无评论