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?

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.org/Class_Reference/wpdb#INSERT_row

  • big thanks to you! You really helped me out i swear u i checked all possible things, but this little mistake i didn't recognize so thank you! Also i will follow your suggestion in just using a string instead of an array ;-) – user3714751 Mar 14 '16 at 17:03
  • Glad i'm of help :) – donMhi.co Mar 16 '16 at 6:56
0

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')
);
  • also thank you for your answer, donMhi.co was a little bit faster :-) – user3714751 Mar 14 '16 at 17:02
  • oh yes sorry my mistake, but he also mentioned that i should use only a string if the data types are equal. – user3714751 Mar 14 '16 at 17:06
  • No worries at all buddy :) Good luck! – Sumit Mar 14 '16 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.