skip to Main Content
$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.

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
    }

2

Answers


  1. Use this instead of $wpdb->update()

    $wpdb->query($wpdb->prepare("UPDATE $table_name SET tvalue='1' WHERE id=%d", 1));
    
    Login or Signup to reply.
  2. 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search