skip to Main Content

I have a table named ‘species’ with 3 columns: id, specie, rate.

And I have tried using the following query but it throws the errors:

Fatal error: Uncaught Error: Call to a member function insert() on null in D:Wordpress....includesspecie_form.php on line 10

Error: Call to a member function insert() on null in D:Wordpress.....includesspecie_form.php on line 10

Query

$specie_name= $_POST['specie_name'];
$specie_rate=$_POST['specie_rate'];

    global $wpdb;
    
    $wpdb->insert('species', array( //this is line 10 of error
        'id' => NULL,
        'specie' => '$specie_name',
        'rate' => '$specie_rate'
    ));

This is what my db looks like:
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    Ok so I managed to solve the issue by including some needed files like so:

     global $wpdb;
    
        if(!isset($wpdb))
            {
                require_once('../../../../wp-config.php');
                require_once('../../../../wp-includes/wp-db.php');
            }
    
        $wpdb->insert('species', array(
            'specie' => $specie_name,
            'rate' => $specie_rate
        ));
    

  2. it look like in table species you cannot enter null as a valid value to id column .

    look in the table if column id is nullable or not
    and try to give him a number

    if there is an autoincrese option try insert without the id column

    $wpdb->insert('species', array(
        'specie' => '$specie_name',
        'rate' => '$specie_rate'
    ));
    

    this is not the problem with the query but how you access wpdb see

    Call to a member function insert() on null WORDPRESS

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search