php - 将元素存储到数据库中
问题描述
我有一个动态创建的表。意味着标题是固定的,并且行数据将在某些 ajax 调用后动态附加。我的问题是如何在数据库中使用 product_id(即<tr>
id)保存数量值?
<tr>
<th></th>
<th>Product Name</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total </th>
</tr>
// 这部分将在我使用自动完成搜索的 ajax 调用之后追加
<tr id="+ui.item.id+"> // product_id
<td><i class='flaticon-delete-1 delete-row' onclick='deleteRow(this)'></i></td>
<td>"+ui.item.value+"</td>
<td>"+ui.item.unit_price+"</td>
<td><input type='text' class='quantity' value='1'></td> //quantity
<td class='total'>"+ui.item.unit_price+"</td>
</tr>
解决方案
将数量和产品存储在数组中:
<td><input type='text' name="id[]" class='productclass' value='+ui.item.id+'></td>
<td><input type='text' name="quantity[]" class='quantity' value='1'></td>
如果您不想显示产品 ID,可以隐藏:
<input type='hidden' name="id[]" class='productclass' value="+ui.item.id+">
在服务器端:
//Check input data
if(in_array("",$_POST['id'], true)){
//Empty id
}else{
$countid = count($_POST['id']);
for($j=0;$j<$countid;$j++){
$id = $_POST['id'][$j];
$quantity = $_POST['quantity'][$j];
//code for saving into the database
}
}
推荐阅读
- json - 如何使用存储在不同数据类型的变量中的数据创建 json 对象
- javascript - JQGrid 减少窗口调整大小事件的列数
- javascript - 如何找到两个值之间的差异?
- sql - Teradata 会话中止 AMP CPU 阈值
- sql - How to subtract the current figure from the previous figure in ms access
- pdf - iTextSharp parse table
- reactjs - 为什么 next/link 渲染两次?如何解决?
- ios - firebase_admob - 严格将此插件用于广告时,pod 会不必要地仅在 iOS 端安装 firebase 核心和其他插件
- javascript - 无法识别 webpack-dev-server
- android - Kotlin 类中的字符串请求