Title: Problems with $wpdb
Last modified: February 12, 2021

---

# Problems with $wpdb

 *  [klaivu](https://wordpress.org/support/users/klaivu/)
 * (@klaivu)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/problems-with-wpdb-2/)
 * I’m trying to change some entries in the wpdb, to change the status of reservations
   in a booking calendar to approved on a successful payment. It doesn’t seem to
   work, and I really have no clue as to why. I’m pretty new to wordpress, php and
   javascript, so it’s probably something really silly.
 * Here’s my code:
 * Functions.php:
 *     ```
       function set_db_transient() {
            if(isset($_COOKIE['user_key'])){
               set_transient($_COOKIE['user_key'] . 'database_id', $_POST['database_id'], 600); 
            }
            wp_die();
       }
   
       add_action( 'wp_ajax_set_db_transient', 'set_db_transient' );
       add_action( 'wp_ajax_nopriv_set_db_transient', 'set_db_transient' );
   
       function filter_woocommerce_payment_successful_result() {     
           global $wpdb;
           $change_status = $wpdb->update($wpdb->prefix . 'reservations_table_name', array('status' => "approved"), array('id' => get_transient($_COOKIE['user_key'] . 'database_id')));    
       }; 
   
       add_filter( 'woocommerce_payment_complete', 'filter_woocommerce_payment_successful_result');
       ```
   
 * JS ajax call:
 *     ```
       database_id = $("input[name='reservationid']").val();			
       jQuery.post( 'wp-admin/admin-ajax.php', { 'action': 'set_db_transient', 'database_id': database_id });
       ```
   
    -  This topic was modified 5 years, 4 months ago by [klaivu](https://wordpress.org/support/users/klaivu/).

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/problems-with-wpdb-2/#post-14043159)
 * Don’t use relative URLs in WP related code. There will be situations where what
   is relative will be wrong. Use a full, absolute URL to post Ajax requests. You
   can use `admin_url()` to construct a full URL.
 * Get your basic code working first, but you’ll eventually need to validate and
   sanitize any data coming in from the browser. Never add unsanitized data to your
   DB, it’s a major security risk. To further secure the exchange, include a nonce
   value in the served page, which is then sent along with the Ajax request for 
   your Ajax handler to verify. Otherwise a bad actor could submit malicious Ajax
   data from anywhere.
 * Use your browser’s network developer tool to verify the request is properly formed.
   Verify data within your Ajax handler is as expected by sending debug data to 
   the error log with `error_log()`.

Viewing 1 replies (of 1 total)

The topic ‘Problems with $wpdb’ is closed to new replies.

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [transient](https://wordpress.org/support/topic-tag/transient/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/problems-with-wpdb-2/#post-14043159)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
