Make WordPress Core


Ignore:
Timestamp:
11/16/2021 02:57:53 AM (3 years ago)
Author:
hellofromTonya
Message:

WPDB: Capture error in wpdb::$last_error when insert fails instead of silently failing for invalid data or value too long.

Instead of silently failing when attempting to insert a value into a field, this commit saves the error in the wpdb::$last_error property.

Sets last_error with an error message if:

  • wpdb::query() fails for invalid data
  • wpdb::process_fields() fails to process the value(s) for the field(s) where the value could be too long or contain invalid data

Sets last_query if wpdb::query() fails for invalid data.

If __() is not available, uses non-translated error message to ensure the error is captured.

There is no change to wpdb aborting when an error occurs.

Adds tests.

Props dlt101, mnelson4, dd32, pento, hellofromTonya, davidbaumwald, sergeybiryukov, johnbillion, swissspidy, datainterlock, anandau14, anthonyeden, asif2bd, audrasjb, chaion07, dpegasusm, fpcsjames, galbaras, jdgrimes, justindocanto, kwisatz, liammitchell, lucasw89, lukecarbis, nettsite, nlpro, procodewp, psufan, richardfoley, skunkbad, travisnorthcutt, woodyhayday, zoiec.
Fixes #37267.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r51919 r52176  
    20162016            $this->flush();
    20172017            if ( $stripped_query !== $query ) {
    2018                 $this->insert_id = 0;
     2018                $this->insert_id  = 0;
     2019                $this->last_query = $query;
     2020
     2021                if ( function_exists( '__' ) ) {
     2022                    $this->last_error = __( 'WordPress database error: Could not perform query because it contains invalid data.' );
     2023                } else {
     2024                    $this->last_error = 'WordPress database error: Could not perform query because it contains invalid data.';
     2025                }
     2026
    20192027                return false;
    20202028            }
     
    25362544
    25372545        if ( $data !== $converted_data ) {
     2546
     2547            $problem_fields = array();
     2548            foreach ( $data as $field => $value ) {
     2549                if ( $value !== $converted_data[ $field ] ) {
     2550                    $problem_fields[] = $field;
     2551                }
     2552            }
     2553
     2554            if ( 1 === count( $problem_fields ) ) {
     2555                if ( function_exists( '__' ) ) {
     2556                    /* translators: %s Database field where the error occurred. */
     2557                    $message = __( 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.' );
     2558                } else {
     2559                    $message = 'WordPress database error: Processing the value for the following field failed: %s. The supplied value may be too long or contains invalid data.';
     2560                }
     2561            } else {
     2562                if ( function_exists( '__' ) ) {
     2563                    /* translators: %s Database fields where the error occurred. */
     2564                    $message = __( 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.' );
     2565                } else {
     2566                    $message = 'WordPress database error: Processing the value for the following fields failed: %s. The supplied value may be too long or contains invalid data.';
     2567                }
     2568            }
     2569
     2570            $this->last_error = sprintf( $message, implode( ', ', $problem_fields ) );
     2571
    25382572            return false;
    25392573        }
Note: See TracChangeset for help on using the changeset viewer.