Insert into wordpress database - Call to a member

2020-05-07 16:15发布

Im having an issue with inserting data into the wordpress database through my plugin. My plugin uses a shortcode to display a form from which some calculations are made and then that information is the then supposed to be inserted into the database but I end up getting this message.

I can't seem to get it to work?.. Any help would be appreciated greatly.

PLUGIN CODE used for the insert:

include_once($_SERVER['DOCUMENT_ROOT'].'/wp-config.php' );
$wpdb->insert(
    'wp_wine_bookings',
    array(
        'name' => $_POST['name'], 
        'last_name' => $_POST['lname'],
        'phone_number' => $_POST['phone_num'], 
        'email' => $_POST['email'],
        'booking_status' => "Booked",
        'tour_name' => $_POST['tourname'],
        'tour_date' => $_POST['tourdates'],
        'number_of_people' =>  $_POST['num_ppl'],
        'price' => $_POST['priceget'],
        'state' => $_POST['state'],
        'city' => $_POST['city'],
        'country' => $_POST['country'],
        'mobile' => $_POST['mobile'],
        'preffered_pickup_point' => $_POST['locationpickup'],
        'arrival_date' => $_POST['arrivaldate'],
        'name_on_credit_card' => $_POST['cn'],
        'credit_card_type' => $_POST['type'],
        'payment_date' => date("d-m-y"),
        'occasion' => $_POST['occasion'],
        'requirementsO' => $_POST['requirementsO'],
        'requirementsP' => $_POST['requirementsP']
    ), 
    array( 
        '%s', 
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s'
    ) 
);

PHP ERROR:

Fatal error: Call to a member function insert() on a non-object in /home/user/public_html/wp-content/plugins/BookingWT/booknow.php on line 52

1条回答
够拽才男人
2楼-- · 2020-05-07 16:36

First:
Why you have included the file wp-config.php? you don't need to include this at all.

Second:

You're accessing the instance of wpdb class ($wpdb). Which is in local scope.

You have to make $wpdb global:

global $wpdb;        // <--- making $wpdb as global
$wpdb->insert(
...
...
查看更多
登录 后发表回答