【发布时间】:2021-10-05 15:53:49
【问题描述】:
场景:我正在开发一个销售和库存系统,我现在处于采购订单阶段。我要做的是将复选框数据插入到 item_po 表的数据库中。
场景:我已经生成了所有低于最低库存的商品(需要补充)。我在生成的数据左侧添加了一个复选框(参见图片以供参考)。我想将该数据插入到我的 item_po 表中,并根据为采购订单检查的项目准备生成报告。这可能吗?
这是我当前的插入代码,但它不起作用。
<?php
require_once('../../inc/config/constants.php');
require_once('../../inc/config/db.php');
$itemDetailsSearchSql = 'SELECT * FROM item WHERE stock < itemMinStock';
$itemDetailsSearchStatement = $conn->prepare($itemDetailsSearchSql);
$itemDetailsSearchStatement->execute();
$output = '<table id="itemMinStockReportsTable" class="table table-sm table-bordered table-hover" style="width:100%">
<thead>
<tr>
<th style="width: 10px !important;"><input type="checkbox" name="check" class="cbxMain" onchange="checkMain(this)"/></th>
<th>Lookup Code</th>
<th>Item Name</th>
<th>Current Stock</th>
<th>Unit Cost</th>
<th>Status</th>
<th>Description</th>
<th>Minimum Stock</th>
<th>Vendor</th>
<th>Action</th>
</tr>
</thead>
<tbody>';
// Create table rows from the selected data
while($row = $itemDetailsSearchStatement->fetch(PDO::FETCH_ASSOC)){
$itemNumber = $row['itemNumber'];
$itemName = $row['itemName'];
$stock = $row['stock'];
$unitPrice = $row['unitPrice'];
$status = $row['status'];
$description = $row['description'];
$itemMinStock = $row['itemMinStock'];
$itemVendor = $row['itemVendor'];
$output .= '<tr>' .
'<td>' . ' <input type="checkbox" name="check[]" class="check" '. '</td> '.
'<td>' . $itemNumber . '</td>' .
//'<td>' . $row['itemName'] . '</td>' .
'<td><a href="#" class="itemDetailsHover" data-toggle="popover" id="' . $row['productID'] . '">' . $itemName . '</a></td>' .
'<td>' . $stock. '</td>' .
'<td>' . $unitPrice. '</td>' .
'<td>' . $status . '</td>' .
'<td>' . $description . '</td>' .
'<td>' . $itemMinStock . '</td>' .
'<td>' . $itemVendor . '</td>' .
'<td>' .'<button type="button" id="deleteItem" class="btn btn-danger">Delete</button> '. '</td>' .
'</tr>';
}
$itemDetailsSearchStatement->closeCursor();
$output .= '</tbody>
<tfoot>
<br>
<th> <br> <button type="button" id="addItem" name="btn_po" class="btn btn-success">Proceed to PO</button> </th>
</tfoot>
</table>';
echo $output;
if(isset($_POST['btn_po']))
{
$checkbox = $_POST['check'];
for($i=0;$i<count($checkbox);$i++)
{
$check_id = $checkbox[$i];
$stockSql = 'SELECT stock FROM item WHERE itemNumber=:itemNumber';
$stockStatement = $conn->prepare($stockSql);
$stockStatement->execute(['itemNumber' => $itemNumber]);
if($stockStatement->rowCount() > 0){
//$row = $stockStatement->fetch(PDO::FETCH_ASSOC);
//$quantity = $quantity + $row['stock'];
echo '<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Item already exists in DB. Please click the <strong>Update</strong> button to update the details. Or use a different Item Number.</div>';
exit();
} else {
// Item does not exist, therefore, you can add it to DB as a new item
// Start the insert process
$insertItemSql = 'INSERT INTO item_po(itemNumber,itemName,stock,unitPrice,status,description,itemMinStock,itemMaxStock,itemVendor) VALUES (:itemNumber, :itemName,:stock, :unitPrice, :status, :description, :itemMinStock, :itemVendor)';
$insertItemStatement = $conn->prepare($insertItemSql);
$insertItemStatement->execute(['itemNumber' => $itemNumber, 'itemName' => $itemName, 'stock' => $stock, 'unitPrice' => $unitPrice, 'status' => $status, 'description' => $description, 'itemMinStock' => $itemMinStock, 'itemVendor' => $itemVendor ]);
echo '<div class="alert alert-danger"><button type="button" class="close" data-dismiss="alert">×</button>Item failed to insert in database.</div>';
exit();
}
}
}
?>
【问题讨论】:
-
您还需要将数据包装在
中的 <input>标记中,给 字段数组名称 像name='data1[]'然后使用$_POST或 @ 获取此值987654329@表单提交后。这能回答你的问题吗? Multiple row insert to table if check box is selected。请注意,您的代码对SQL Injections 开放。您应该改用参数化的预处理语句。