我正在从woocommerce管理员处更新,编辑订单详细信息。我有两个问题:
仅更新“下载计数”列我无法捕获任何sql错误但是,在独立的php文件中使用相同的查询时,效果很好。出了什么问题,如何获取sql错误,或者至少是发送的查询?
$table = $wpdb->prefix . \'woocommerce_downloadable_product_permissions\';
$expiry_date = date("Y-m-d", strtotime("+ 30 days"));
$expiry_time = $expiry_date . \' 00:00:10\';
$wpdb->query("UPDATE `$table` SET
`downloads_remaining` = \'1\',
`access_expires` = \'$expiry_time\',
`download_count` = (`download_count` + 1)
WHERE `order_id` = \'$post_id\'");
这里是“独立php版本,它可以:
$link = mysqli_connect($servername, $username, $password, $db);
$post_id = \'219\';
if (!$link) { exit; }
$expiry_date = date("Y-m-d", strtotime("+ 30 days"));
$expiry_time = $expiry_date . \' 00:00:00\';
$table = \'hkmw_woocommerce_downloadable_product_permissions\';
$query = "UPDATE `$table` SET
`downloads_remaining` = \'1\',
`access_expires` = \'$expiry_time\',
`download_count` = (`download_count` + 1)
WHERE `order_id` = \'$post_id\';";
if (!mysqli_query($link,$query)) {
printf("Errormessage: %s\\n", mysqli_error($link));
}
mysqli_close($link);
SO网友:hal
我不得不使用两个单独的查询。$wpdb->update语法无法处理基于现有值的计算。但对于其他两个领域,它是有效的。就像这样:
$table = $wpdb->prefix . \'woocommerce_downloadable_product_permissions\';
$expiry_date = date("Y-m-d", strtotime("+ 30 days"));
$expiry_time = $expiry_date . \' 00:00:00\';
$wpdb->query("UPDATE $table SET
download_count = (download_count + 1)
WHERE order_id = \'$post_id\'");
$wpdb->update( $table, array( \'downloads_remaining\' => \'1\', \'access_expires\' => $expiry_time), array( \'order_id\' => $post_id ), array( \'%s\', \'%s\' ), array( \'%d\' ) );