This error:
Fatal error: Call to a member function insert() on a non-object
Means that $wpdb
isn’t instantiated so you are running your code too early or out of WordPress context– for example, in an exterior PHP file accessed directly, which is what I suspect (Poorly conceived AJAX request perhaps?).
As mentioned, the code has more formats that data elements and your single quotes are going to cause this fail (in a sense) anyway as you will literally be inserting “$email” and “$name” strings into the database.
function insert_record_to_db($name,$email,$dis){
global $wpdb;
$tablename = $wpdb->prefix.'complaint';
$data=array(
'name' => $name,
'email' => $email,
'dis' => $dis
);
$format= array('%s','%s','%s');
$wpdb->insert( $tablename , $data , $format );
};
insert_record_to_db('a','b','c');