删除行 wordpress 自定义数据库表


Delete row wordpress custom database table

我有一个页面,显示了通过PayPal在我的网站上进行的所有交易。我想添加一个可以删除该特定行的按钮。

事务 ID 是唯一的。

当用户单击按钮仅删除该行时,我将如何能够? 还要运行 jQuery 警报以确认删除?

我的代码:

<table class"widefat">
<?php  global $wpdb;
    $result = $wpdb->get_results ( "SELECT * FROM wp_donations" );
    if (!$result){?>
    <tr>
    <td>There are no donations to show</td>
    </tr>
<?php }else{?> 
<thead>
<tr>
 <th>Name</th>
 <th>Email</th>
 <th>Phone Number</th>
 <th>Address</th>
 <th>Amount</th>
 <th>Method</th>
 <th>Date</th>
 <th>Transaction ID</th>
 <th></th>
</tr>
</thead>
  <?php foreach ( $result as $print )   {?>
    <tr>
    <td><?php echo $print->name;?></td>
    <td><?php echo $print->email;?></td>
    <td><?php $phones = $print->phone; if($phones == 0){echo 'No Number Provided';}else{echo $phones;}?></td>
    <td><?php echo $print->address;?></td>
    <td>$<?php echo $print->amount;?></td>
    <td><?php echo $print->method;?></td>
    <td><?php echo $print->dates;?></td>
    <td><?php echo $print->txid;?></td>
    <td><input type="button" id="<?php echo $print->txid;?>" class="delete" title="Delete" value="delete" /></td>
    </tr>
        <?php }?>
       </table>

如果jquery是一个选项,那么它非常简单:

$(".delete").on('click',function(){
$(this).parents("tr").remove();
})

检查这个

我最终运行了一个操作。 使用数据库中AUTO_INCREMENT的唯一 ID。

,然后在页面加载时调用操作。

$actions = array(
            'delete' => sprintf('<a href="?page=%s&action=delete&id=%s">%s</a>', $_REQUEST['page'], $item['id'], __('Delete', 'custom_table_example')),
        );
   function process_bulk_action()
    {
        global $wpdb;
        $table_name = $wpdb->prefix . 'donations'; // do not forget about tables prefix
        if ('delete' === $this->current_action()) {
            $ids = isset($_REQUEST['id']) ? $_REQUEST['id'] : array();
            if (is_array($ids)) $ids = implode(',', $ids);
            if (!empty($ids)) {
                $wpdb->query("DELETE FROM $table_name WHERE id IN($ids)");
            }
        }
    }