插入到wordpress数据库中-调用成员函数insert() [英] Insert into wordpress database - Call to a member function insert()
问题描述
我在通过插件将数据插入wordpress数据库时遇到问题.我的插件使用一个短代码来显示一个表格,通过该表格进行一些计算,然后将该信息插入数据库中,但最终得到此消息.
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.
用于插入的插件代码:
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错误:
致命错误:在非对象中调用成员函数insert()/home/user/public_html/wp-content/plugins/BookingWT/booknow.php在第52行
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
推荐答案
首先:
为什么要包含文件 wp-config.php
?您根本不需要包括它.
First:
Why you have included the file wp-config.php
? you don't need to include this at all.
第二:
您正在访问 wpdb
类( $ wpdb
)的实例.在本地范围内.
You're accessing the instance of wpdb
class ($wpdb
). Which is in local scope.
您必须使 $ wpdb
global
:
You have to make $wpdb
global
:
global $wpdb; // <--- making $wpdb as global
$wpdb->insert(
...
...
这篇关于插入到wordpress数据库中-调用成员函数insert()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!