Home > Net >  Wordpress simple update command is not working with wpdb?
Wordpress simple update command is not working with wpdb?

Time:10-10

$wpdb-> update('Gen3', array( 'tvalue' => "1"), array('id' => 1));

Table:

id name tvalue
25 #25 0

I want to change tvalue to 1. But I cannot get my code to work.

I tried many options and neither of them works.

CodePudding user response:

I highly suggest to read the "Docs" first. It'll get your problem solved much faster. It's all in here:

UPDATE rowsDocs

Also in your snippet, you've set your 'id' => 1 which is incorrect according to the table you've provided, the id you're interested in, is 25!

Also make sure, you're using the right format for the values in the query. It's very important to use '%s' and '%d' correctly, otherwise it won't work!

Note, I capitalized the ID of your row according to the documentation page, if it does not work, then use the lowercase id.

$wpdb->update(
  'Gen3', // This should be the name of your table
  array(
    'tvalue' => '1',  // string with quotation  // integer (number) without quotation
  ),
  array('ID' => 25), // The id of the row you're trying to update
  array(
    '%s'   // The format of the value you're trying to update. // Use '%d' if it's a number
  ),
  array('%d') // The format of the where clause which is the id of the row you're trying to update
);

Let me know if you were able to get it to work!

CodePudding user response:

Use this instead of $wpdb->update()

$wpdb->query($wpdb->prepare("UPDATE $table_name SET tvalue='1' WHERE id=%d", 1));
  • Related