Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 976
Arm_M Avatar asked Sep 25 '26 13:09

Arm_M


1 Answers

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

like image 147
Machavity Avatar answered Sep 27 '26 02:09

Machavity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!