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.
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?
Try changing up that last array from
array( '%d'
) intoarray( '%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.