函数wpdb :: prepare()的参数太少

时间:2018-03-15 18:36:07

标签: php wordpress

我试图使用ajax调用在wordpress中向数据库添加一些数据。

代码如下:

<?php

    function saveRefreshData(){

        global $wpdb;

        $table_name = $wpdb->prefix . "refresh";

        $state = $_POST["state"];
        $duration = $_POST["duration"];
        $only_for_facebook = $_POST["only_for_facebook"];

        $start_at_1 = $_POST["start_at_1"];
        $start_at_db_1 = !empty($start_at_1) ? "'$start_at_1'" : "NULL";
        $active_duration_1 = !empty($_POST["active_duration_1"]) ? $_POST["active_duration_1"] : "NULL";
        $repeat_daily_1 = $_POST["repeat_daily_1"];

        $start_at_2 = $_POST["start_at_2"];
        $start_at_db_2 = !empty($start_at_2) ? "'$start_at_2'" : "NULL";
        $active_duration_2 = !empty($_POST["active_duration_2"]) ? $_POST["active_duration_2"] : "NULL";
        $repeat_daily_2 = $_POST["repeat_daily_2"];

        $start_at_3 = $_POST["start_at_3"];
        $start_at_db_3 = !empty($start_at_3) ? "'$start_at_3'" : "NULL";
        $active_duration_3 = !empty($_POST["active_duration_3"]) ? $_POST["active_duration_3"] : "NULL";
        $repeat_daily_3 = $_POST["repeat_daily_3"];

        $start_at_4 = $_POST["start_at_4"];
        $start_at_db_4 = !empty($start_at_4) ? "'$start_at_4'" : "NULL";
        $active_duration_4 = !empty($_POST["active_duration_4"]) ? $_POST["active_duration_4"] : "NULL";
        $repeat_daily_4 = $_POST["repeat_daily_4"];

        if($wpdb->query($wpdb->prepare ("UPDATE $table_name SET
                                                state = ". $state .",
                                                duration = ". $duration .",
                                                only_for_facebook = ". $only_for_facebook .",
                                                start_at_1 = " . $start_at_db_1 . ",
                                                active_duration_1 = ". $active_duration_1 .",
                                                repeat_daily_1 = ". $repeat_daily_1 .",
                                                start_at_2 = ". $start_at_db_2 .",
                                                active_duration_2 = ". $active_duration_2 .",
                                                repeat_daily_2 = ". $repeat_daily_2 .",
                                                start_at_3 = ". $start_at_db_3 .",
                                                active_duration_3 = ". $active_duration_3 .",
                                                repeat_daily_3 = ". $repeat_daily_3 .",
                                                start_at_4 = ". $start_at_db_4 .",
                                                active_duration_4 = ". $active_duration_4 .",
                                                repeat_daily_4 = ". $repeat_daily_4 .",
                                                added_time = now()"))){

            wp_send_json_success( 200 );

        }else{

            wp_send_json_error( 400 );

        }

    }

    add_action("wp_ajax_save_refresh", "saveRefreshData");

?>

但我收到以下错误:

2018/03/15 17:40:07 [error] 47449#47449: *530638 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught ArgumentCountError: Too few arguments to function wpdb::prepare(), 1 passed in /var/www/clients/client12/web19/web/wp-content/plugins/boky/ajax/save-refresh.php on line 48 and exactly 2 expected in /var/www/clients/client12/web19/web/wp-includes/wp-db.php:1222
Stack trace:
#0 /var/www/clients/client12/web19/web/wp-content/plugins/boky/ajax/save-refresh.php(48): wpdb->prepare('UPDATE wp_refre...')
#1 /var/www/clients/client12/web19/web/wp-includes/class-wp-hook.php(286): saveRefreshData('')
#2 /var/www/clients/client12/web19/web/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
#3 /var/www/clients/client12/web19/web/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#4 /var/www/clients/client12/web19/web/wp-admin/admin-ajax.php(97): do_action('wp_ajax_save_re...')
#5 {main}
  thrown in /var/www/clients/client12/web19/web/wp-includes/wp-db.php on line 1222

直到最后一次wordpress更新,一切都很好。

有什么想法吗?

0 个答案:

没有答案