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.
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 ?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With