Forums

Call to a member function insert() on a non-object (7 posts)

  1. Extrix
    Member
    Posted 1 year ago #

    Hello,

    I am simply trying to insert a row into my custom table I put in the WP DB, however when I try it, it gives me the error in the topic title. I have set $wpdb as global so I'm not sure why it's still not working. I am a newbie still at PHP.

    Thanks a lot.

    <?php
    
    global $table;
    global $footer_slogan;
    global $wpdb;
    
    $table = $wpdb->prefix . 'customtable';
    
    $footer_slogan = $_POST['footer_slogan'];
    
    function test_insert_data() {
    
    	global $wpdb;
    	global $table;
    	global $footer_slogan;
    
    	$data = array('element_name'=>'Footer Slogan',
    					'element_show'=>1,
    					'element_content'=>$footer_slogan);
    
    	$wpdb->insert( $table, $data );
    }
    
    test_insert_data();
    
    ?>
  2. Extrix
    Member
    Posted 1 year ago #

    I forgot to add I am passing some form information to this file to process.

  3. Extrix
    Member
    Posted 1 year ago #

    Can anyone take a stab at this issue? The only problem I can foresee is that I am not putting $wpdb in a global scope, but I am and still get the error. Anyone have any input?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Is it not more simple to use the option Api to do this kind of thing.

  5. Extrix
    Member
    Posted 1 year ago #

    I would like to store all my information in my own table since there will be a lot of options...

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Yes, but you can store all your options in an array.

    something like this:

    $theOptions = array('media_per_page' => 20,
    'show_title' => true,
    'show_alternate' => true,
    'show_caption' => true,
    'show_description' => true,
    'page_links_all' => 'page_links_all',
    'page_links' => '',
    'use_filter_on_posts' => true,
    'update_post_content' => false,
    'big_thumbnails' => false
    );
    update_option('all_my_options', $theOptions);

    And if you want your options all you have to do is:

    $my_options = get_option('all_my_options');
    and you have acces to all your options. WordPress will escape all your options which is also a benefit.

  7. Extrix
    Member
    Posted 1 year ago #

    Thanks, I'll give it a try.

Topic Closed

This topic has been closed to new replies.

About this Topic