我试图使用php向数据库添加一些数据。我的代码如下:
$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()"))
这在过去有效。但由于某些原因它不再起作用,我得到一个错误:
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
因此,我需要使用sprintf
占位符并在数组中传递参数。我尝试按如下方式进行:
$state = $_POST["state"]; // That is int (0 or 1)
$duration = $_POST["duration"]; // That is int
$only_for_facebook = $_POST["only_for_facebook"]; // That is int (0 or 1)
$start_at_1 = $_POST["start_at_1"]; // That is timestamp
$start_at_db_1 = !empty($start_at_1) ? "'$start_at_1'" : "NULL"; // That is timestamp or NULL
$active_duration_1 = !empty($_POST["active_duration_1"]) ? $_POST["active_duration_1"] : "NULL"; // That is int or NULL
$repeat_daily_1 = $_POST["repeat_daily_1"]; // That is int (0 or 1)
$wpdb->prepare ("UPDATE $table_name SET
state = %d,
duration = %d,
only_for_facebook = %d,
start_at_1 = %s,
active_duration_1 = %d,
repeat_daily_1 = %d,
added_time = %s"),
array( $state,
$duration,
$only_for_facebook,
$start_at_db_1,
$active_duration_1,
$repeat_daily_1,
now())))
以下是数据库中的样子:
但它不起作用。知道为什么吗?