Make WordPress Core

Opened 6 years ago

Closed 3 months ago

#44163 closed defect (bug) (duplicate)

wpdb->update() fails, showing no error message

Reported by: tazotodua's profile tazotodua Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Database Keywords: reporter-feedback
Focuses: Cc:


It's my second report about the incompletness of wpdb->update function.
1-2 years ago I've reported the scenario when it failed to update, but last_error was empty.

Now, another scenario, when it fails , but last_error is empty, is the following:

i.e. create a table with "NOT NULL"

$x= $wpdb->query("CREATE TABLE IF NOT EXISTS `example_table` (
                `rowID` int(11) NOT NULL AUTO_INCREMENT,
                `name` varchar(150) NOT NULL,
                `id` varchar(150) NOT NULL,
                `data` LONGTEXT  NOT NULL DEFAULT '',
                PRIMARY KEY (`rowID`),
                UNIQUE KEY `rowID` (`rowID`)
                ) ". $wpdb->get_charset_collate() ." AUTO_INCREMENT=1;"

then insert some empty value:

 $wpdb->insert("example_table", array("name" => "James", "id"=>"123", "data"=>"blabla") );

then try to update

 $wpdb->update("example_table", array("name"=>"Jonathan"),  array("id"=>"133") );

it doesnt update.
I have dumped $this->dbh (in wp-db.php 1887 line), and it shows affected lines: 0

last_error is empty. After spending much time, found that I was missing "required" parameter and added

 $wpdb->update("example_table", array("name"=>"Jonathan", "data"=>"new-blabla"),  array("id"=>"133") );

("NOT NULL" requirement caused failure)

so, it's good, last_error contained some information, when affected lines are 0, or like such scenarios.

Change History (5)

#1 @SergeyBiryukov
6 years ago

  • Component changed from General to Database

#2 follow-up: @subrataemfluence
6 years ago

  • Keywords reporter-feedback added

Your insert code is:

$wpdb->insert("example_table", array("name" => "James", "id"=>"123", "data"=>"blabla") );

and Update code is:

$wpdb->update("example_table", array("name"=>"Jonathan"),  array("id"=>"133") );

Not sure if you have used different id intentionally in insert and update. I think it should be 123!

$wpdb->update("example_table", array("name"=>"Jonathan"),  array("id"=>"123") );

I tested with the following and worked perfectly for me.

$wpdb->insert("example_table", array("name" => "James", "id"=>"135", "data"=>"blabla") );
$wpdb->update("example_table", array("name"=>"Subrata"),  array("id"=>"135") );

#3 in reply to: ↑ 2 @tazotodua
6 years ago

sorry, yeah, 133 was typo. However, i had problem.
maybe something with my mysql installation? have to check.

Version 1, edited 6 years ago by tazotodua (previous) (next) (diff)

#4 @subrataemfluence
6 years ago

Thank you for your feedback.
Would you mind getting back to me with a specific example so that I can reproduce the issue and look deeper into it?

#5 @hellofromTonya
3 months ago

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

Related to #32315.

[52176] populated the wpdb::$last_error (instead of silently failing) when data is invalid or too long. It modified the wpdb::process_fields() logic which is also used by wpdb::update(). In testing an insert and update, it is recording the error message in wpdb::$last_error. I suspect this changeset also resolved this ticket.

Given there's been no follow-up, I'll mark it as a duplicate of #32315. However, if the problem persists, please provide step-by-step instructions of how to reproduce and reopen the ticket. Thank you.

Note: See TracTickets for help on using tickets.