想要更新数据库中的值。但它在NULL上显示了UPDATE()

时间:2018-02-13 作者:Fokrule

我想更新数据库表中的列值。我有一个表单,通过提交此表单,我将重定向到另一个名为verify.php 并尝试更新列值。但它表示对null上的成员函数update()的调用。这是我的表单代码:

<form action="verify.php" method="POST">
<input type="text" value="\'.esc_attr($result->wp_user_id).\'" name="providerId">
<input type="text" name="verify">
<button> Verify</button>
</form>
在我的verify.php 密码

<?php
global $wpdb;
//$postData = extract($_POST);
    if ($_SERVER[\'REQUEST_METHOD\'] == \'POST\') {
        //global $wpdb;
        $wpdb->update(\'service_finder_providers\',
            array(
                \'is_nidverified\' => \'Yes\'
            ),
            array( \'id\' => 60 ),
                array( 
                    \'%s\'
                ), 
        array( \'%s\' ) 
);
}
它表示对null上的成员函数update()的调用

1 个回复
SO网友:sandrodz

你的verify.php 文件位于WordPress框架之外,因此$wpdb和其他一切都不可用。

正确的方法是使用admin_post_ 钩你可以读到它here.

这在函数中进行。php

add_action( \'admin_post_add_foobar\', \'prefix_admin_add_foobar\' );

function prefix_admin_add_foobar() {
    //$wpdb->update()
}
以及表格:

<form action="<?php echo esc_url(admin_url(\'admin-post.php\')); ?>" method="post">
  <input type="hidden" name="action" value="add_foobar">
  <input type="hidden" name="data" value="foobarid">
  <input type="submit" value="Submit">
</form>
更简单,但not recommended 解决方案是在您的verify.php 文件,在顶部添加(将../../../../替换为文件的正确路径):

require_once(\'../../../wp-load.php\');

结束

相关推荐

Database Name Change

我读到,使用wp\\uu之外的其他东西作为数据库前缀更安全,所以我下载了我的数据库前缀wp\\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu