Support » Plugins » Hacks » 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)
  • 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' );

    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.

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

    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!

    As an update…from
    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, 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.

    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:

    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.

    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.