wordpress wpdb update difficulties

Suppose I have a table in my wp database fruits:

            CREATE TABLE `fruits` (
                `fruit` varchar(99),
                `istasty` tinyint(1)
            )

            INSERT INTO `fruits` (`fruit`, `istasty`) VALUES
            ('pear', 1),
            ('peach', 1),
            ('rotten strawberry', 1),
            ('strawberry', 1),
            ('banana', 1),
            ('apple', 1);

the goal is to update istasty to 0 in the ‘rotten strawberry’ row.

Read More

the class reference for wpdb in the wordpress codex gives:

$wpdb->update( $table, $data, $where, $format = null, $where_format = null );

so I’ve attempted the following:

            $wpdb->update( 
                'fruits', 
                array( 
                    'fruit' => 'rotten strawberry',
                    'istasty' => '0'
                ), 
                array( 'fruit' => 'rotten strawberry' ), 
                array( 
                    '%s',   // value1
                    '%d'    // value2
                ), 
                array( '%d' ) 
            );

Apparently this updates EVERY record to ‘rotten strawberry’, ‘1’.

What am I doing wrong here?

Related posts

1 comment

  1. Try changing up that last array from array( '%d' ) into array( '%s' ). You are now telling the script that the where variable is an integer, which it is not.

    Also you could remove 'fruit' => 'rotten strawberry' from your first array. In this array you place whatever you want to update. And all you want to do is set istasty to 0.

Comments are closed.