首页 > 解决方案 > 如何通过ID删除数据库中的数据

问题描述

我仍在尝试学习如何使用数据库服务器。

我想就如何传递 id 值以在我的数据库/表中删除、添加和编辑列寻求帮助。

这是我的代码:

jQuery( document ).ready(function() {
var table = jQuery('#example').dataTable({

         "bProcessing": true,
         "sAjaxSource": "server/data2.php",
          "bPaginate":true,
          "sPaginationType":"full_numbers",
          "iDisplayLength": 15,

         "aoColumns": [
                { mData: 'INVOICE' },
                { mData: 'PRODUCT' },
                { mData: 'SIZE' },
                { mData: 'DATE' },
                { mData: 'DDATE' },
                { mData: 'SUPLIER' },
                { mData: 'COST' },
                { mData: 'STATUS' }
        ],
            "columnDefs": [ 
          {   
            "aTargets":[8],  // this your column of action
            "mData": null, 
            "mRender": function(data, type, full){
             return '<div id="container"><a class="btn btn-info btn-sm" href="javascript: void(0);" class="click_'+full[0]+'" title="Click to PRINT">PRINT</a></div>';   // replace this with button 
            }
          }
         ]
});   

这是我的桌子

<table id="example" class="table table-striped table-bordered table-hover" width="100%" cellspacing="0">
    <thead>
        <tr>

            <th>INVOICE</th>
            <th>Product Name</th>
            <th>SIZE</th>
            <th>DATE ORDER</th>
            <th>DATE DELIVER</th>
            <th>SUPPLIER</th>
            <th>COST</th>
            <th>STATUS</th>
            <th>FORM</th>

        </tr>
    </thead>

</table>

这是我的 sql 从数据库中调用数据

$sql = "Select s.invoice_number as INVOICE, s.date_order as DATE, s.suplier as SUPLIER, s.date_deliver as DDATE, CONCAT(d.product_name, d.color) as PRODUCT, s.qty as QTY, s.cost as COST, s.status as STATUS, d.size_id as SIZEFROM purchases sINNER JOIN products d on d.product_id=s.p_name WHERE STATUS = 'received'LIMIT 100000";

$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
    $data[] = $rows;
}
$results = array(
    "sEcho" => 1,
    "iTotalRecords" => count($data),
    "iTotalDisplayRecords" => count($data),
    "aaData"=> $data
);
echo json_encode($results);
exit;

我还在学习如何使用数据库。

感谢那些可以提供建议的人:)

标签: phpmysqldatatableserver-side

解决方案


如何通过ID删除数据库中的数据

SQL 查询字符串在您的 PHP 代码中应如下所示:

$sql = 'DELETE FROM invoice WHERE id = <ID>';

这个问题得到了回答,但是我看到你还有其他一些问题。

例如,您需要将这些 ID 存储在某个地方,这样您就知道要删除什么。由于您有一个表,我假设您想用数据库中的数据填充它。为此,基本的 SQL 查询字符串将如下所示 - 如果您不使用任何过滤:

$sql = 'SELECT * FROM invoice';

然后你将把结果放到你的表中,如下所示:

<table id="example" class="table table-striped table-bordered table-hover" width="100%" cellspacing="0">
    <thead>
    <tr>
        <th>INVOICE</th>
        <th>Product Name</th>
        <th>SIZE</th>
        <th>DATE ORDER</th>
        <th>DATE DELIVER</th>
        <th>SUPPLIER</th>
        <th>COST</th>
        <th>STATUS</th>
        <th>FORM</th>
        <th></th>
    </tr>
    <?php
        foreach ($invoices as $invoice) {
            echo "<tr id='row-{$invoice['id']}'>
                    <td>{$invoice['invoice_number']}</td>
                    <td>{$invoice['product_name']}</td>
                    <td>{$invoice['some_field']}</td>
                    <td>{$invoice['some_other_field']}</td>
                    <td>{$invoice['ect']}</td>
                    // ...
                    <td><button class='delete-btn' id='{$invoice['id']}'>Delete</button></td>
                 </tr>";
        }
    ?>
    </thead>
</table>

注意额外的列。这是您可以放置​​按钮的地方,就像我使用“删除”按钮所做的那样。另请注意,按钮的 id 属性是数据库中发票的 id。对此有更多更好的解决方案,但首先它会很好,因为它很容易理解。

我之所以把那个ID放在那里,是因为像这样,如果你想删除HTML表中的行,你可以用javascript/jQuery获取按钮的ID。

例子:

$(document).ready(function(){
    $('body').on('click', 'button.delete-btn', function(events){
        let id = $(this).attr('id');
        $.post("invoice.php", {
            id
        });

        $('#row-' + id).remove();
    });
});

单击按钮时会触发此 jQuery 函数。它获取您单击的对象的 id 属性(按钮),然后使用 id 参数向 invoice.php 发送一个发布请求 - 这是您执行 DELETE 查询字符串并将此 ID 传递给您从请求中获得的 WHERE 条件。最后,jQuery 函数从 DOM 中删除该行。


推荐阅读