WordPress wpdb INSERT (9 posts)

  1. Ryan
    Posted 5 years ago #

    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) );
  2. Michael Fields
    Posted 5 years ago #

    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' );
  3. Ryan
    Posted 5 years ago #

    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.

  4. Michael Fields
    Posted 5 years ago #

    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 if it is long.

  5. Ryan
    Posted 5 years ago #

    Here is my code for the Admin of the plugin:
    Options Page

    And here is the relevant code from the called PHP file:
    Called File

    Many thanks for any help Michael!

  6. Ryan
    Posted 5 years ago #

    As an update...from http://codex.wordpress.org/AJAX_in_Plugins:
    First, add some javascript that will trigger the AJAX request:

    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);

    Then, set up a PHP function that will handle that request:

    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;

    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.

  7. Michael Fields
    Posted 5 years ago #

    Does the WordPress environment exist in Called File? 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:


  8. Michael Fields
    Posted 5 years ago #

    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.

  9. Ryan
    Posted 5 years ago #

    Thanks Michael! I will take a look at that and make the required mods to try and get this going.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.