WordPress.org

Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#53297 closed defect (bug) (invalid)

Bug in wpdb update

Reported by: ttodua Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Database Keywords:
Focuses: Cc:

Description

To test a simple example:

 $wpdb->update('any_custom_table', ['parent'=>"hello world", "other"=>"xyz"], $WhereArray );

Even though, in the table, the "parent" column is "text" type (non numeric), wordpress still converts the value and instead of "hello world" the numeric value is being inserted (i.e. 0 in that case, however, if you pass "123456_hello", it inserts 123456.

Change History (4)

#1 @pbiron
2 months ago

Hi @ttodua. In your case, you need to use the 4th parameter ($format) of wpdb:update() to specify the "format" of the data.

For example:

$wpdb->update(
   'any_custom_table',
   array(
      'parent' => 'hellow world',
      'other'  => 'xyz',
   ),
   array(
      '%s',
      '%s',
   )
);

Why? Because wp_posts has a column named post_parent (with an "alias" of parent). wpdb, by default, treats any column named parent as being an integer (even in custom tables).

For details of all column names whose values are not treated as strings, see wp_set_wpdb_vars().

#2 @ttodua
2 months ago

Thanks for excellent answer!

#3 @pbiron
2 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

My pleasure. I'm going to close this ticket.

#4 @pbiron
2 months ago

For anyone finding this ticket in the future, let me correct one thing in my answer above.

The reason columns named parent are treated as integers is because wp_term_taxonomy has a column named parent which is an integer (and not because parent is an "alias" of the post_parent column in wp_posts). My bad.

The rest of the answer (about using the $format parameter) is correct :-)

Note: See TracTickets for help on using tickets.