通过POST执行SQL查询

时间:2018-12-27 13:58:15

标签: wordpress woocommerce

我已经设置了一个自定义设置页面,并在以下部分下添加了一个字段

add_settings_field("delete-commands", "Delete Commands", "delete_commands", "demo", "section");
register_setting("section", "delete_commands");

提交删除按钮后,想执行delete_commands功能

enter image description here

以下代码在提交后并没有删除订单,刷新了设置页面。想知道出了什么问题,谢谢。

function delete_commands()
{
  global $wpdb;
  ?>
  <form method="post">
   <input class="btn" style="float:none;" type="submit"  name="delete" value="Delete all orders"  />
 </form>

  <?php
  if (isset($_POST['delete'])) {
        //global $wpdb;
        $table_name = $wpdb->prefix;
        $wpdb->query(
          "DELETE  FROM {$table_name}_woocommerce_order_itemmeta;
           DELETE FROM  {$table_name}_woocommerce_order_items;
           DELETE FROM  {$table_name}_comments WHERE comment_type = 'order_note';
           DELETE FROM  {$table_name}_postmeta WHERE post_id IN ( SELECT ID FROM  {$table_name}_posts WHERE post_type = 'shop_order' );
           DELETE FROM  {$table_name}_posts WHERE post_type = 'shop_order';
      ");

    }
}

0 个答案:

没有答案