Call to a member function insert() on null WORDPRESS

I’m writing a plugin for wordpress. I just finished my admin page. It has a submit button. The action attribute of form element is sending to “database.php” file.

Here’s the code of database.php:

Read More
<?php
global $wpdb;

        $table_name =  $wpdb->prefix . "ArmPoll_Questions";
    if($_SERVER["REQUEST_METHOD"]=="POST")
    {

        $wpdb->insert($table_name, array('id'=>'','Question'=>'How r u?'));

    }
?>

But it leads to an error which says

Call to a member function insert() on null

Related posts

Leave a Reply

2 comments

  1. You can’t just write a straight up PHP page and expect it to work. You need to write this as a plugin or something that works within the framework of WordPress. The error you’re getting is that the $wpdb object isn’t defined. If you write it as a plugin $wpdb will be defined for you automatically.

    How to Write a WordPress Plugin

    The other alternative is to open your own MySQL connection and do the insertion directly that way.

    How to insert data using mysqli

  2. You can load core features where wpdb is defined with

    define( 'SHORTINIT', true );
    require( BASE_PROY_ROOT.'/wp-load.php' );
    

    The SHORTINIT set to “true” if for load only the basic of WP CORE.
    Then you can use
    global $wpdb;