【问题标题】:Insert into wordpress database - Call to a member function insert()插入 wordpress 数据库 - 调用成员函数 insert()
【发布时间】:2014-03-19 01:39:21
【问题描述】:

我在通过我的插件将数据插入 wordpress 数据库时遇到问题。 我的插件使用简码来显示一个表单,从中进行一些计算,然后该信息应该被插入到数据库中,但我最终收到了这条消息。

我似乎无法让它工作?..任何帮助将不胜感激。

用于插入的插件代码:

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 行

【问题讨论】:

  • 您的错误不言自明:对象 $wpdb 上不存在函数 insert()。 $wpdb 设置在哪里?

标签: php database wordpress sql-insert


【解决方案1】:

第一:
为什么包含文件wp-config.php?你根本不需要包含这个。

第二:

您正在访问 wpdb 类 ($wpdb) 的实例。在本地范围内。

你必须使$wpdb global:

global $wpdb;        // <--- making $wpdb as global
$wpdb->insert(
...
...

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 2014-09-27
    • 1970-01-01
    • 2020-12-15
    • 2020-08-04
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多