I know this is an old question that is repeated here a lot. but I couldn't find an answer that works, and my code is really bad. How can i update multiple rows in one query when i have to use two keys to run through in my query. I saw a lot of answers. But no one worked for me.

my table:

id| entry  | meta_key | meta_value
1 |    1   |     2    |   input1
2 |    1   |     3    |   input2
3 |    1   |     4    |   input3
4 |    2   |     5    |   input4

i'm updating the table using the entry as an id. but the code is repetitive. the only pice of data that is fixed is the meta_key. and will not change.

$result = $wpdb->query("UPDATE `table_name` SET `meta_value`= '".$meta_value1."' WHERE `entry` = '".$entry."'  AND `meta_key` = 2");
$result = $wpdb->query("UPDATE `table_name` SET `meta_value`= '".$meta_value2."' WHERE `entry` = '".$entry."'  AND `meta_key` = 3");

how can i make this code good.

P.S my code works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.