简体   繁体   中英

Fatal error Call to a member function insert() on a non-object

I am trying to insert data using ajax in WordPress.But I am getting error below is my code in the file which I have called using ajax:

global $wpdb;
                     $wpdb->insert('design_information',  
                      array( 
                      'layout' => $_SESSION['layout'], 
                      'language' => $_SESSION['briefform_language'], 
                      'logo_name' => $_SESSION['briefform_businessName_validation'] ,
                      'org_description' => $_SESSION['briefform_businessPurpose'], 
                      'bussiness_industry' => $_SESSION['briefform_businessIndustry'] ,
                      'slogan_logo' => $_SESSION['briefform_slogan'], 
                      'payment' => $_SESSION['price'],
                      'others' => $_SESSION['briefform_comments'], 
                      'fullname' => $_POST['paymentform_contactDetails_fullName'] ,
                      'company_name' => $_POST['paymentform_contactDetails_companyName'], 
                      'coutry_code' => $_POST['paymentform_contactDetails_phoneCountry'] ,
                      'payment_status' => 'pending',
                      'color_picker' => $newValue1,
                      'phone_no' => $_POST['paymentform_contactDetails_phoneNumber'], 
                              'address1' => $_POST['paymentform_billingDetails_address1'] , 
                      'address2' => $_POST['paymentform_billingDetails_address2'] ,
                      'city' => $_POST['paymentform_billingDetails_city'],
                      'zip' => $_POST['paymentform_billingDetails_zip'],
                      'state' => $_POST['paymentform_billingDetails_state'], 
                              'country' => $_POST['paymentform_billingDetails_country'],
                              'design' => $newValue, 
                              'slider' => $slider_value, 
                              'Website Address' => $_SESSION['website address'], 
                              'style' => $_SESSION['style'], 
                              'like' => $_SESSION['like'],
                              'file' => $_SESSION['file'], 
                              'email' => $_SESSION['email'],  
                              'order_status' => "pending"
               ));   

Um, it's weird, maybe the php thinks that variable hasn't been declared or is not an instance:

Try this:

include_once('wp-includes/wp-db.php');

global $wpdb;
$wpdb->insert('design_information',  ...

It is a wordpress feature, so the global $wpdb can be called only if it has been defined previously.

This means, inside the /wp-content/... folder if you use a script with wp specific features it won't work until it is compiled with wordpress core.

So, if you are working on standalone script than it will not work. if you want to use wordpress function then you need to load wordpress

if you are working in theme or plugin then below should work

global $wpdb;
$wpdb->insert();

… or access it per $GLOBALS …

$GLOBALS['wpdb']->insert();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM