提问人:grey 提问时间:5/10/2020 最后编辑:Littmgrey 更新时间:5/10/2020 访问量:809
如何传递 ID 以删除数据库中的数据
How to pass ID to delete data in database
问:
我仍在尝试学习如何使用数据库服务器。
我想就如何传递 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;
我仍在学习如何使用数据库。
感谢那些可以提供建议的人:)
答:
1赞
Random Dude
5/10/2020
#1
如何传递 ID 以删除数据库中的数据
在 PHP 代码中,SQL 查询字符串应如下所示:
$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发送 post 请求 - 这是您执行 DELETE 查询字符串并将此 ID 传递给您从请求中获得的 WHERE 条件的文件。最后,jQuery 函数从 DOM 中删除该行。
评论