WordPress $wpdb update not working when mounted inside function?

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

Table:

idnametvalue
25#250

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

I tried many options and neither of them works.

This is my full code:

add_action( 'wp_footer', 'mycustom_wp_footer' );

    function mycustom_wp_footer() {
        ?>
        <script type="text/javascript">
            var wpcf7Elm = document.querySelectorAll( '.wpcf7' );
            wpcf7Elm.forEach(function(formr){
            formr.addEventListener( 'wpcf7submit', function( event ) {
                    //if ('wpcf7-f101-p97-o1' == event.detail.unitTag) {

                        $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
                        );
                                            //}
                    //var idform = event.detail.unitTag;
                    //alert (idform);
            }, false ); })
        </script>
        <?php
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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!

Method 2

Use this instead of $wpdb->update()

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x