Title: WordPress wpdb INSERT
Last modified: August 19, 2016

---

# WordPress wpdb INSERT

 *  [Ryan](https://wordpress.org/support/users/rfrankel/)
 * (@rfrankel)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/)
 * Does anyone see anything wrong with what I am doing here? I haven’t worked with
   the database expressions in WP yet. All of my code compiles except when I start
   to try to add items to tables I have created in the WP database. Any info would
   be greatly appreciated.
 *     ```
       $wpdb->insert('venue', array('event_id' => $event_id,
       	'event_date' => $event_date,
       	'venue_name' => $venue_name,
       	'venue_city' => $venue_city,
       	'ticket_url' => $ticket_url,
       	'event_url' => $event_url),
         array(%d, %s, %s, %s, %s, %s) );
       ```
   

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741569)
 * I rarely use custom tables, but in one of my last projects it was necessary. 
   One thing that I learned was that you need to register the table with the $wpdb
   object. Something like this May help you out:
 *     ```
       function my_register_custom_database_tables() {
       	global $wpdb;
       	$wpdb->my_table_name = $wpdb->prefix . 'my_table_name';
       }
       add_action( 'init', 'my_register_custom_database_tables' );
       ```
   
 *  Thread Starter [Ryan](https://wordpress.org/support/users/rfrankel/)
 * (@rfrankel)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741570)
 * Thanks. I was able to successfully add the tables and register them in the initialization
   of the plugin. The problem seems to come from writing to the tables during an
   AJAX call.
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741573)
 * It is possible that the issue stems from a bug in the Ajax implementation. Using
   the code you posted above, can you write to the table with no Ajax? How do you
   have your ajax callbacks set up? Please post code to [http://wordpress.pastebin.com](http://wordpress.pastebin.com)
   if it is long.
 *  Thread Starter [Ryan](https://wordpress.org/support/users/rfrankel/)
 * (@rfrankel)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741750)
 * Here is my code for the Admin of the plugin:
    [Options Page](http://wordpress.pastebin.com/ZA42BzYA)
 * And here is the relevant code from the called PHP file:
    [Called File](http://wordpress.pastebin.com/HL7aRhd2)
 * Many thanks for any help Michael!
 *  Thread Starter [Ryan](https://wordpress.org/support/users/rfrankel/)
 * (@rfrankel)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741751)
 * As an update…from [http://codex.wordpress.org/AJAX_in_Plugins](http://codex.wordpress.org/AJAX_in_Plugins):
   ——–
   First, add some javascript that will trigger the AJAX request:
 *     ```
       <?php
       add_action('admin_head', 'my_action_javascript');
       function my_action_javascript() {
       ?>
       <script type="text/javascript" >
       jQuery(document).ready(function($) {
   
       	var data = {
       		action: 'my_special_action',
       		whatever: 1234
       	};
   
       	// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
       	jQuery.post(ajaxurl, data, function(response) {
       		alert('Got this from the server: ' + response);
       	});
       });
       </script>
       <?php
       }
       ```
   
 * Then, set up a PHP function that will handle that request:
 *     ```
       <?php
       add_action('wp_ajax_my_special_action', 'my_action_callback');
       function my_action_callback() {
       	global $wpdb; // this is how you get access to the database
       	$whatever = $_POST['whatever'];
       	$whatever += 10;
               echo $whatever;
       	die();
       }
       ```
   
 * —–
    Is this how it should be done? I can see there are some differences in my
   implementation, mostly that it looks like this is triggered using some WP hook
   instead of how I did it with a user clicking. To me though, it looks like the
   example would fire everytime the page is loaded.
 * Once again any info would be greatly appreciated.
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741753)
 * Does the WordPress environment exist in [Called File](http://wordpress.pastebin.com/HL7aRhd2)?
   From what you posted, it does not look like it does. Ajax calls are best handled
   in WordPress using /wp-admin/admin-ajax.php Two really great places to start 
   are:
 * [http://codex.wordpress.org/AJAX](http://codex.wordpress.org/AJAX)
    [http://www.wphardcore.com/2010/5-tips-for-using-ajax-in-wordpress/](http://www.wphardcore.com/2010/5-tips-for-using-ajax-in-wordpress/)
 *  [Michael Fields](https://wordpress.org/support/users/mfields/)
 * (@mfields)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741754)
 * Sorry, I was still typing when you made your last post 🙂 Yes this is the proper
   way to handle Ajax calls. Most likely your problem stems from not having the 
   $wpdb global defined in the original callback script. Using admin-ajax.php should
   solve this.
 *  Thread Starter [Ryan](https://wordpress.org/support/users/rfrankel/)
 * (@rfrankel)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741755)
 * Thanks Michael! I will take a look at that and make the required mods to try 
   and get this going.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘WordPress wpdb INSERT’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 8 replies
 * 2 participants
 * Last reply from: [Ryan](https://wordpress.org/support/users/rfrankel/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/wordpress-wpdb-insert/#post-1741755)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
