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

问题描述 投票:-2回答:1

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

我想寻求有关如何传递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;

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

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

php mysql datatable server-side
1个回答
0
投票

如何通过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传递给您从请求中获得的条件。最后,jQuery函数从DOM中删除该行。

© www.soinside.com 2019 - 2024. All rights reserved.