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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With