【问题标题】:Delete row wordpress custom database table删除行wordpress自定义数据库表
【发布时间】:2015-01-22 14:41:03
【问题描述】:

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

交易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>

【问题讨论】:

    标签: php jquery mysql wordpress


    【解决方案1】:

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

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

    查看this

    【讨论】:

    • 欣赏它 - 尝试从数据库中删除该行
    • 是的 - 我知道我需要在单击按钮后运行一个函数 - wpdb-&gt;delete 只是不知道该怎么做
    • 很简单,看看这个:stackoverflow.com/questions/5967322/…
    • @Mark 你需要使用 ajax 来传递相应的行,是什么让每一行都独一无二?
    • 交易ID。你发布的问题对我没有帮助。
    【解决方案2】:

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

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

    $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)");
                }
            }
        }
    

    【讨论】:

    • 嗨,马克,我无法理解您的代码。你能解释一下吗?我无法理解删除在 wpdb 中的工作原理
    猜你喜欢
    • 2017-06-04
    • 2011-06-05
    • 2014-09-20
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 2011-03-24
    • 1970-01-01
    相关资源
    最近更新 更多