Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress 4.2.2 update - failing wpdb->insert

context

I have been working on a new wordpress blog as a personal website. Part of it, I have a custom contact form where people put in their details to get in touch with me.It has been working good till morning, after which I have updated to 4.2.2v citing security reasons.

problem

After the update, the form is failing to save any of the information into the DB. The $wpdb->insert_id is returning 0. The query is the same, the page is the same, everything is just the same. The only change is I have upgraded to 4.2.2v from 4.2.1v.

Is there any issue with the recent update or do I have to do any more steps after the word press manual update ?

debugging done...

I have ensured that the DB version is updated. It is showing 31535. When debugging using the $wpdb->lastquery and $wpdb->print_error() I get

WordPress database error: []
SHOW FULL COLUMNS FROM `wp_tst_tbl_contacts`

?

I could not understand what is wrong here. If I run the same insert query, as well as the above show full columns on command line using the same user wp user credentials, it works perfectly.

note: If there is anymore information needed, please ask.

like image 585
Sanjeev Avatar asked May 07 '15 16:05

Sanjeev


People also ask

What is global $Wpdb?

WordPress provides a global object, $wpdb , which is an instantiation of the wpdb class. By default, $wpdb is instantiated to talk to the WordPress database. The recommended way to access $wpdb in your WordPress PHP code is to declare $wpdb as a global variable using the global keyword, like this: Copy.

How do I find the last insert ID in WordPress?

If you want to get the last inserted row ID from the WordPress database. You can use the $wpdb->insert() it does the insert. $lastid = $wpdb->insert_id; You can find more information about how to do things the WordPress way can be found in the WordPress codex.


2 Answers

I found the problem cause. Its due to a column width limitation.

I have a VARCHAR(9) column and I was sending data that is 16 char length. The new change in the 4.2.2 gets the table meta and crops the data such that it fits properly into the size of the column as defined in DB. And it also compares the pre-crop and post-crop data. If they don't match, it is failing.

The problem is, its failing silently with no error thrown. I have found this via debugging the wpincludes/wp-db.php file.

Please check your column limit and the column data length you send.

Once I have increased the column width (as the data will definitely be more than 9 chars), the issue got resolved.

like image 176
Sanjeev Avatar answered Oct 06 '22 22:10

Sanjeev


I was experiencing this same issue and it turned out to be some unescaped values being pushed into the database from a csv import function.

I applied the proper esc_url() and / or esc_attr() and / or esc_html() where appropriate to sanitize the values pre-insertion and then the query ran successfully.

like image 40
Patrick S Perkins Avatar answered Oct 06 '22 23:10

Patrick S Perkins