致命错误:在非对象上调用成员函数 get_results()(jQuery 来自插件和 WordPress)

发布于 2024-12-05 18:50:52 字数 851 浏览 0 评论 0原文

我正在尝试在 Wordpress 插件中使用 jQuery 的 Form 插件。我正在关注这个示例。我已经将我的脚本排入队列并构建了我的表单。在 csf_form_handler.php(相当于示例的 json-echo.php)中,我可以访问表单中选择的项目(我有一个单选按钮组)。

我的目标是使用 SELECT 语句中表单中选择的值从自定义 WordPress 数据库表返回数据。

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

global $wpdb;

$csf_db_table = $wpdb->prefix . "activity";


$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

不幸的是,我得到:

注意:尝试获取非对象的属性(在 $wpdb-> 前缀上) 线)

致命错误:在非对象上调用成员函数 get_results() (在 $csf_data 行上)

csf_form_handler.php 中的上述代码不在函数中。我不知道这是否有什么不同。

如何更改代码以便可以使用 $wpdb?

谢谢。

I'm trying to use jQuery's Form plugin with in a Wordpress plugin. I'm following this example. I've enqueued my scripts and built my form. In csf_form_handler.php, the equivalent of the example's json-echo.php, I can access the items selected in my form (I have a radiobutton group).

My goal is to use the values selected in the form in a SELECT statement to return data from a custom wordpress database table.

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

global $wpdb;

$csf_db_table = $wpdb->prefix . "activity";


$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

Unfortunately, I'm getting:

Notice: Trying to get property of non-object (on the $wpdb->prefix
line)

Fatal error: Call to a member function get_results() on a non-object
(on the $csf_data line)

The code above in csf_form_handler.php isn't in a function. I don't know if that makes a difference.

How can I change the code so that I can use $wpdb?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

古镇旧梦 2024-12-12 18:50:52

我遇到了同样的问题,但我通过包含 WordPress 文件夹根目录中的 wp-config.php 文件解决了这个问题,如下所示:

require_once('../../../wp-config.php');
global $wpdb;

我希望这会有所帮助。

I came across the same issue but I got it resolved by including the wp-config.php file from the root of the WordPress folder like this:

require_once('../../../wp-config.php');
global $wpdb;

I hope this helps.

浅语花开 2024-12-12 18:50:52

编写插件时,最好将数据处理保留在插件的主文件中(即不将其发送到单独的文件),并激活函数来相应地处理它。基本上,您可以将表单的操作设置为指向插件的文件或包含表单的页面。

假设您正在处理的此表单显示在网站前端的侧边栏上。要在用户单击“提交”时处理来自该表单的数据,您可以在我们的插件文件中创建一个函数,例如:

function $csf_get_data(){

global $wpdb; //since your this function is in your plugin’s file, $wpdb should be available, so no errors here! =)

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

$csf_db_table = $wpdb->prefix . "activity";

$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

    //do your stuff with $csf_data
}

//now run it everytime the plugin is run
if(isset($_POST[‘submit’])){
    $csf_get_data();
}

现在,您可以设置表单操作的属性以将数据发送到同一页面,这将能够使用上面的函数来处理它。您可以使用:

    action=””

    action="<?php the_permalink()?>"

请注意:为了确保数据来自您的网站(尤其是公共表单),请记住使用 wp_nonce_field() 创建一个可由 wordpress 通过 wp_nonce() 验证的随机数字段: http://codex.wordpress.org/Function_Reference/wp_nonce_field

希望有帮助,

Vq。

When writing plugins, it's a good idea to keep your data-handling within the plugin's main file (i.e. not sending it to a separate file), and activate functions to handle it accordingly. Basically, you can set your form's action to point to either the plugin's file, or the page that contains the form.

Let’s assume this form you are working on is displayed on the front end of the site, on the sidebar. To handle data coming from that form when a user clicks “submit”, you could create a function in our plugin's file such as:

function $csf_get_data(){

global $wpdb; //since your this function is in your plugin’s file, $wpdb should be available, so no errors here! =)

$csf_selected_sport = $_POST['csf_radiobutton_group_sport'];

$csf_db_table = $wpdb->prefix . "activity";

$csf_data = $wpdb->get_results($wpdb->prepare("
            SELECT *
            FROM " .$csf_db_table. "
            WHERE  " . $csf_selected_sport ." ")); 

    //do your stuff with $csf_data
}

//now run it everytime the plugin is run
if(isset($_POST[‘submit’])){
    $csf_get_data();
}

Now, you can set up your form action’s property to send the data to the same page, which will be able to handle it using the function above. You could use either:

    action=””

or

    action="<?php the_permalink()?>"

Please note: to make sure the data is coming from your site (especially public forms), remember to use wp_nonce_field() to create a nonce field that can be validated byt wordpress via wp_nonce(): http://codex.wordpress.org/Function_Reference/wp_nonce_field

Hope that helps,

Vq.

泅人 2024-12-12 18:50:52

你在哪里运行这段代码?听起来您正在创建 $wpdb 实例(或超出范围)之前运行它。

您如何加载 csf_form_handler.php 文件?您是否将其包含在 WordPress 中作为脚本的一部分,或者它是您自己的插件?如果是后者,请记住您需要先激活它,以便 WP 可以将其包含在加载序列中(我假设它已经激活,但是 JIC)

要查看 $wpdb 对象是否已经创建,您可以运行 打印出对象的内容。

让我了解更多信息,希望能够帮助您。

Where are you running this code? It sounds like you are running it BEFORE the $wpdb instance is created (or is out of scope).

How are you loading your csf_form_handler.php file? Are you including as part of a script within Wordpress, or is it your own plugin? If it's the latter, remember that you need to activate it first, so WP can include it in the loading sequence (I'm assuming it's already activated, but JIC)

To see if the $wpdb object has already been created, you could run <?php print_r($wpdb);?> to print out the object's contents.

Let me know more, and hopefully I'd be able to help you.

南风起 2024-12-12 18:50:52

我收到这个致命错误是因为我忘记将

global $wpdb;

$wpdb->get_results() 函数放入文件中。

I got this fatal error because I forgot to put

global $wpdb;

inside the file where I put the $wpdb->get_results() function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文