3

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:

<?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

Machavity
  • 30,841
  • 27
  • 92
  • 100
Arm_M
  • 93
  • 2
  • 7

2 Answers2

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

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
0

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;

ClaudioC
  • 1,505
  • 13
  • 10