Make WordPress Core

Opened 6 years ago

Last modified 6 years ago

#44163 new defect (bug)

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

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

Description

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"

<?php
$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 (4)

#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:

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

and Update code is:

<?php
$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!

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

I tested with the following and worked perfectly for me.

<?php
$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. There seems problem on my side.

However, my main concert is that there are cases, when update fails, but last_error is empty. That's what I wished to be solved. in wp-db.php in update function, there are cases like that, when last_error is not being set, but returning false.

Last edited 6 years ago by tazotodua (previous) (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?

Note: See TracTickets for help on using tickets.