我正在用html和php创建一个表单。我已经创建了一个表单,我想提交并保存在数据库中的数据。
我正在尝试提交一个表单,其中包含来自while循环的数据。所有输入值都由while循环生成。
代码如下所示。
<table width="1348" border="0" class="table table-striped" >
<tr>
<td width="106"> </td>
<td width="332"><strong>Product Code</strong></td>
<td width="375"><strong>Product Name</strong></td>
<td width="211"><strong>QTY</strong></td>
</tr>
<?php
$i = 0;
$rowset = mysql_query("select * from product_detail where productID='".$data['productCode']."'");
while($stuff = mysql_fetch_array($rowset)){
?>
<tr>
<td><input type="text" name="code[<?php echo $i?>]" value="<?php enter code hereecho $stuff['code'];?>"/></td>
<td><input type="text" name="name[<?php echo $i?>]" value="<?php echo $stuff['name'];?>" size="50"/></td>
<td><input type="text" name="qty[<?php echo $i?>]" value="<?php echo $stuff['qty'];?>" size="10"/></td>
</tr>
<?php $i++; }?>
<tr id="last">
</table>
<input type="submit" name="save id="save" class="btn btn-primary btn-lg"/>
这是将数据添加到数据库的代码。
$code=$_POST['code'.$i];
$name=$_POST['name'.$i];
$qty=$_POST['qty'.$i];
$query = mysqli_query($con,"insert into stock(productCode, productName, qty) values ('".$code."', '".$name."','".$qty."')") or die(mysqli_error($con));
2条答案
按热度按时间qc6wkl3g1#
首先,使用带有bind_param的prepared语句,因为脚本完全暴露于sql注入。
第二,可以为行数添加隐藏的输入类型
与表格一起过帐
在插入之前,您可能还需要通过其他必要的验证来检查post值是否为空
ej83mcc02#
由于表是动态填充的,因此需要使用数组作为name属性
php应该是这样的,假设数据中有值:
要查看数组的内容,请使用print\r($tablerow)在本例中,我使用一个名称tablerow