admin管理员组文章数量:1130349
I am doing an update in from woocommerce admin, edit order details. I have two problems:
- Only column 'download-count' is updated.
- I am not able to catch any sql errors.
However, when using the same query in a standalone php file, it works fine. What is wrong, and how can I get hold of the sql errors, or at least the query sent?
$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'");
Here is "standalone php version, which works:
$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);
I am doing an update in from woocommerce admin, edit order details. I have two problems:
- Only column 'download-count' is updated.
- I am not able to catch any sql errors.
However, when using the same query in a standalone php file, it works fine. What is wrong, and how can I get hold of the sql errors, or at least the query sent?
$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'");
Here is "standalone php version, which works:
$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);
Share
Improve this question
edited Jan 30, 2017 at 19:49
hal
asked Jan 30, 2017 at 14:25
halhal
351 gold badge1 silver badge9 bronze badges
5
|
1 Answer
Reset to default 2I had to use two separate queries. The $wpdb->update syntax cant deal with calculating based on the existing value. But for the other two fields it works. So like this:
$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' ) );
I am doing an update in from woocommerce admin, edit order details. I have two problems:
- Only column 'download-count' is updated.
- I am not able to catch any sql errors.
However, when using the same query in a standalone php file, it works fine. What is wrong, and how can I get hold of the sql errors, or at least the query sent?
$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'");
Here is "standalone php version, which works:
$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);
I am doing an update in from woocommerce admin, edit order details. I have two problems:
- Only column 'download-count' is updated.
- I am not able to catch any sql errors.
However, when using the same query in a standalone php file, it works fine. What is wrong, and how can I get hold of the sql errors, or at least the query sent?
$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'");
Here is "standalone php version, which works:
$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);
Share
Improve this question
edited Jan 30, 2017 at 19:49
hal
asked Jan 30, 2017 at 14:25
halhal
351 gold badge1 silver badge9 bronze badges
5
- I don't see how this query can work in any situation. this seems like an invalid php code. – Mark Kaplun Commented Jan 30, 2017 at 15:39
- @MarkKaplun, Really? It works in a standalone php file? Why wouldn't it work? – hal Commented Jan 30, 2017 at 17:13
- hmmm, maybe it is the way you formatted the code.... but what is this "standalone" you are talking about, there shoiuld be zero difference, unless the code is different. My guess is that with loging on you will spot the problem in no time – Mark Kaplun Commented Jan 30, 2017 at 18:08
- I inserted the php-file in the main post above. Same query, but without any $wpdb->query wrapper. – hal Commented Jan 30, 2017 at 19:52
-
therefor it is not the same code.... you should probably install the query monitor plugin and see what query is actually being sent. In any case
$wpdbhas anupdatemethod you might want to use to improve the readability – Mark Kaplun Commented Jan 31, 2017 at 4:21
1 Answer
Reset to default 2I had to use two separate queries. The $wpdb->update syntax cant deal with calculating based on the existing value. But for the other two fields it works. So like this:
$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' ) );
本文标签: wp adminwpdb update query in plugin only updating one column
版权声明:本文标题:wp admin - $wpdb update query in plugin only updating one column 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749101475a2316056.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$wpdbhas anupdatemethod you might want to use to improve the readability – Mark Kaplun Commented Jan 31, 2017 at 4:21