【发布时间】:2015-12-16 20:57:11
【问题描述】:
我有一个带有动态列的 HTML 表格,可根据用户的选择增加或减少。 从前端的用户那里获取输入后,我需要将此 HTML 表插入到 MySQL 数据库表中。
从类别到产品名称的列是恒定的,位置列根据用户要求而变化。
一旦输入完成并点击保存,我需要以以下格式将此数据插入到生产表中:
到目前为止我已经完成的代码请指导我正确的方向:
<?php
if(isset($_POST['for_post_market'])){ $market = $_POST['for_post_market']; }
if(isset($_POST['for_post_prod_date'])){ $date_prod = $_POST['for_post_prod_date']; }
if(isset($_POST['for_post_sale_date'])){ $date_sale = $_POST['for_post_sale_date']; }
$query = 'SELECT * FROM product WHERE prod_status="Active"';
$stmt = $DB_con->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['prod_cat_name']][] = $row['prod_id'];
}
?>
<form method="post">
<table id="invoices" border="1" class="table table-striped table-bordered">
<thead>
<col width="65">
<col width="65">
<th>Category</th>
<th>Product ID</th>
<th>Product Name</th>
<th hidden="true">Production Date</th>
<th hidden="true">Sales Date</th>
<?php
$a=count($market);
for($i=0;$i<$a;$i++) {
echo '<th><input type="hidden" value="'. $market[$i] . '">'. $market[$i] .'</th>';
}
?>
</thead>
<tbody>
<?php
foreach($result as $id => $invoices) {
echo '<tr>';
echo '<td rowspan='. count($invoices) . '>' . $id . '</td>';
$count = 0;
foreach ($invoices as $invoice) {
if ($count != 0) {
echo '<tr>';
}
$count++;
echo '<td>' . $invoice . '</td>';
?>
<?php
$psql = $DB_con->prepare("SELECT * FROM product WHERE prod_id='$invoice'");
$psql->execute();
$resultpro = $psql->fetchall(PDO::FETCH_ASSOC);
foreach($resultpro as $line) {
}
?>
<td><?php echo $line['prod_name']; ?></td>
<?php
echo '<td hidden="true">' . $date_prod . '</td>';
echo '<td hidden="true">' . $date_sale . '</td>';
$a=count($market);
for($j=0;$j<$a;$j++) {
echo '<td><input type="text" name="'. $market[$j] .' "class="form-control" maxlength="6" size="4"></td>';
}
}
}
echo '</tbody>';
echo '</table>';
?>
<button type="submit" class="btn btn-default waves-effect waves-light" name="btn-saveforcast" id="btn-saveforcast">Save</button>
</form>
<?php
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
else
{
?>
完整代码Link
【问题讨论】:
-
能否格式化您的代码?
-
我确实使用了代码标签
-
您是否希望通过此代码并删除所有空的 IF 和 FOR 循环,匹配所有
{和},以便我们确定您认为自己在做什么在我们尝试提出任何建议之前