我有一些数组或复选框,并且选中的值需要写入mysql表。截至目前它还没有写入数据库。所以我怀疑我是否采取了正确的方式。下面的代码来自db的一系列记录。现在它有2行复选框。它可以是一个,两个或三个,具体取决于数据。
我的html如下:
<form action="comparitive_state_comprehensive_update.php?tender_id=1063" method="POST"></form>
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="7" cellspacing="0" width="160">
<tbody>
<tr>
<td height="30" width="170">Export Charges:</td>
</tr>
<tr>
<td height="30">IATA Charges:</td>
</tr>
<tr>
<td height="30">COC Charges:</td>
</tr>
<tr>
<td height="30">Calibration Charges:</td>
</tr>
<tr>
<td height="30">Third Party Insp. Chrg</td>
</tr>
<tr>
<td height="30">Ultrasonic Insp. Chrg</td>
</tr>
<tr>
<td height="30">Optional Access Cost</td>
</tr>
<tr>
<td height="30">Extended Warranty Chrg</td>
</tr>
<tr>
<td height="30">Installation & Training</td>
</tr>
<tr>
<td height="30">Approx. Freight</td>
</tr>
<tr>
<td height="30">Other Charges</td>
</tr>
</tbody>
</table>
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0" width="100">
<tbody>
<tr>
<td height="30" width="540">12 GBP <input name="chk[]" id="exportinr" value="12" type="checkbox"></td>
</tr>
<tr>
<td height="30">34 GBP <input name="chk[]" id="iatainr" value="34" type="checkbox"></td>
</tr>
<tr>
<td height="30">78 GBP <input name="chk[]" id="cocinr" value="7848.0783407565" type="checkbox"></td>
</tr>
<tr>
<td height="30">54 GBP <input name="chk[]" id="calibrationinr" value="5433.2850051391" type="checkbox"></td>
</tr>
<tr>
<td height="30">98 GBP <input name="chk[]" id="tpinr" value="9860.4061204376" type="checkbox"></td>
</tr>
<tr>
<td height="30">32 GBP <input name="chk[]" id="ultrasoninr" value="3219.7244474898" type="checkbox"></td>
</tr>
<tr>
<td height="30">19 GBP <input name="chk[]" id="optinr" value="1911.7113906971" type="checkbox"></td>
</tr>
<tr>
<td height="30">25 GBP <input name="chk[]" id="extinr" value="2515.4097246014" type="checkbox"></td>
</tr>
<tr>
<td height="30">67 GBP <input name="chk[]" id="instinr" value="6741.2980619318" type="checkbox"></td>
</tr>
<tr>
<td height="30">16 GBP <input name="chk[]" id="freightinr" value="1609.8622237449" type="checkbox"></td>
</tr>
<tr>
<td height="30">39 GBP <input name="chk[]" id="otherinr" value="3924.0391703782" type="checkbox"></td>
</tr>
</tbody>
</table>
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0" width="100">
<tbody>
<tr>
<td height="30" width="540">98 GBP <input name="chk[]" id="exportinr" value="8768.88" type="checkbox"></td>
</tr>
<tr>
<td height="30">23 GBP <input name="chk[]" id="iatainr" value="4565.98" type="checkbox"></td>
</tr>
<tr>
<td height="30">89 GBP <input name="chk[]" id="cocinr" value="76576.098" type="checkbox"></td>
</tr>
<tr>
<td height="30">21 GBP <input name="chk[]" id="calibrationinr" value="87687.090" type="checkbox"></td>
</tr>
<tr>
<td height="30">90 GBP <input name="chk[]" id="tpinr" value="24565.989" type="checkbox"></td>
</tr>
<tr>
<td height="30">28 GBP <input name="chk[]" id="ultrasoninr" value="223.090" type="checkbox"></td>
</tr>
<tr>
<td height="30">42 GBP <input name="chk[]" id="optinr" value="5465.09" type="checkbox"></td>
</tr>
<tr>
<td height="30">65 GBP <input name="chk[]" id="extinr" value="1213.89" type="checkbox"></td>
</tr>
<tr>
<td height="30">12 GBP <input name="chk[]" id="instinr" value="87678.88" type="checkbox"></td>
</tr>
<tr>
<td height="30">19 GBP <input name="chk[]" id="freightinr" value="76576.98" type="checkbox"></td>
</tr>
<tr>
<td height="30">10 GBP <input name="chk[]" id="otherinr" value="78687687" type="checkbox"></td>
</tr>
</tbody>
</table>
<input type='submit' name='submit' value='Add Items to Quotation'>
</form>
</div>
我的PHP脚本在下面插入数据:
$chkbox = $_POST['chk'];
$exportinr = filter_input(INPUT_POST, 'exportinr');
$iatainr = filter_input(INPUT_POST, 'iatainr');
$cocinr = filter_input(INPUT_POST, 'cocinr');
$calibrationinr = filter_input(INPUT_POST, 'calibrationinr');
$tpinr = filter_input(INPUT_POST, 'tpinr');
$ultrasoninr = filter_input(INPUT_POST, 'ultrasoninr');
$optinr = filter_input(INPUT_POST, 'optinr');
$extinr = filter_input(INPUT_POST, 'extinr');
$instinr = filter_input(INPUT_POST, 'instinr');
$freightinr = filter_input(INPUT_POST, 'freightinr');
$otherinr = filter_input(INPUT_POST, 'otherinr');
$currency2 = $_POST['currency2'];
$i = 0;
While($i<sizeof($chkbox))
{
mysqli_query($con,"INSERT INTO quotation_terms (tender_id, export_charges_inr, iata_charges_inr, coc_charges_inr, calibration_charges_inr, tp_inspe_charges_inr, ultrason_inspe_charges_inr, opt_access_cost_inr, ext_warranty_cost_inr, inst_training_cost_inr, approx_freight_inr, other_charges_inr, currency1)
VALUES ('$tender_id','$exportinr','$iatainr','$cocinr','$calibrationinr','$tpinr','$ultrasoninr', '$optinr', '$extinr', '$instinr', '$freightinr', '$otherinr', '$currency2')");
$i++;
}
echo "Checkbox value is successfully submitted.";
这是实际的PHP Form脚本:
while($list1=mysql_fetch_array($add1))
{
echo '<tr><input type="checkbox" name="chk[exportinr]" id="exportinr" value='.$list1['export_charges'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[iatainr]" id="iatainr" value='.$list1['iata_charges'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[cocinr]" id="cocinr" value='.$list1['coc_charges_inr'].'></td></tr> ';
echo '<tr><input type="checkbox" name="chk[calibrationinr]" id="calibrationinr" value='.$list1['calibration_charges_inr'].'></td></tr>';
echo '<tr><input type="checkbox" name="chk[tpinr]" id="tpinr" value='.$list1['tp_inspe_charges_inr'].'></td></tr>';
echo '<tr><input type="checkbox" name="chk[ultrasoninr]" id="ultrasoninr" value='.$list1['ultrason_inspe_charges_inr'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[optinr]" id="optinr" value='.$list1['opt_access_cost_inr'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[extinr]" id="extinr" value='.$list1['ext_warranty_cost_inr'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[instinr]" id="instinr" value='.$list1['inst_training_cost_inr'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chkfreightinr[]" id="freightinr" value='.$list1['approx_freight_inr'].'></td></tr>';
echo '<tr><td height="30"><input type="checkbox" name="chk[otherinr]" id="otherinr" value='.$list1['other_charges_inr'].'></td></tr>';
}
答案 0 :(得分:0)
您将获得$_POST['chk']
$checkedArr = $_POST['chk'];
答案 1 :(得分:0)
提交复选框后,您会获得数组。您需要内爆或解析该值并存储在数据库中。
$checkedArr = $_POST['chk']; //return an array.
$checkboxvalue = implode(",", $checkedArr);
然后将$checkboxvalue
插入mysql数据库。并且您将以逗号分隔的形式获得价值。然后在从数据库获取时,您可以分解db值并使用foreach
来显示复选框的值。
答案 2 :(得分:0)
您需要像这样整理表单(注意$i
递增和放置密钥):
<form action="comparitive_state_comprehensive_update.php?tender_id=1063" method="POST">
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="7" cellspacing="0" width="160">
<tbody>
<tr>
<td height="30" width="170">Export Charges:</td>
</tr>
<tr>
<td height="30">IATA Charges:</td>
</tr>
<tr>
<td height="30">COC Charges:</td>
</tr>
<tr>
<td height="30">Calibration Charges:</td>
</tr>
<tr>
<td height="30">Third Party Insp. Chrg</td>
</tr>
<tr>
<td height="30">Ultrasonic Insp. Chrg</td>
</tr>
<tr>
<td height="30">Optional Access Cost</td>
</tr>
<tr>
<td height="30">Extended Warranty Chrg</td>
</tr>
<tr>
<td height="30">Installation & Training</td>
</tr>
<tr>
<td height="30">Approx. Freight</td>
</tr>
<tr>
<td height="30">Other Charges</td>
</tr>
</tbody>
</table>
<?php
$data[] = array(
"exportinr" => 12,
"iatainr" => 34,
"cocinr" => 7848.0783407565,
"calibrationinr" => 5433.2850051391,
"tpinr" => 9860.4061204376,
"ultrasoninr" => 3219.7244474898,
"optinr" => 1911.7113906971,
"extinr" => 2515.4097246014,
"instinr" => 6741.2980619318,
"freightinr" => 1609.8622237449,
"otherinr" => 3924.0391703782
);
$data[] = array(
"exportinr" => 54,
"iatainr" => 23,
"cocinr" => 234.0783407565,
"calibrationinr" => 3453.2850051391,
"tpinr" => 11111.4061204376,
"ultrasoninr" => 32132.7244474898,
"optinr" => 4433.7113906971,
"extinr" => 5224.4097246014,
"instinr" => 6666.2980619318,
"freightinr" => 231343.8622237449,
"otherinr" => 10303.0391703782
);
$i = 0;
foreach($data as $row) {
?>
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0" width="100">
<tbody>
<?php
foreach($row as $key => $value) { ?>
<tr>
<td height="30" width="540"><?php echo number_format($value,0); ?> GBP <input name="chk[<?php echo $i; ?>][<?php echo $key; ?>]" id="<?php echo $key; ?>" value="<?php echo $value; ?>" type="checkbox" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php $i++;
} ?>
<input type='submit' name='submit' value='Add Items to Quotation' />
</form>
</div>
修改强>
这应该相当于基于你的表格的上述代码。请注意,您应该mysql_
,mysqli_
:
<?php
$i = 0;
while($row = mysqli_fetch_array($add1, MYSQLI_ASSOC)) {
?>
<table class="inlineTable" style="border-collapse: collapse" border="1" bordercolor="#000000" cellpadding="5" cellspacing="0" width="100">
<tbody>
<?php
foreach($row as $key => $value) { ?>
<tr>
<td height="30" width="540"><?php echo number_format($value,0); ?> GBP <input name="chk[<?php echo $i; ?>][<?php echo $key; ?>]" id="<?php echo $key; ?>" value="<?php echo $value; ?>" type="checkbox" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php $i++;
} ?>
提交此内容将为您提供一个易于分组的数组进行处理:
Array
(
[chk] => Array
(
[0] => Array
(
[cocinr] => 7848.0783407565
[tpinr] => 9860.4061204376
)
[1] => Array
(
[iatainr] => 23
[calibrationinr] => 3453.2850051391
)
)
[submit] => Add Items to Quotation
)
将mysql处理成一个查询将是这样的(有很多方法可以通过验证来处理它,这几乎是通过最少的验证发送到DB):
if(!empty($_POST['chk'])) {
// Loop through each row
for($i = 0; $i < count($_POST['chk']); $i++) {
// Get just the values
$arrVals = array_values($_POST['chk'][$i]);
// Run through the values and sanitize for numeric/decimals/commas
$arrVals = array_map(function($v) { $v = preg_replace('/[^0-9\.\,]/',"",$v); return $v; },$arrVals);
// Implode the columns
$setCols = "(`".implode("`,`",array_keys($_POST['chk'][$i]))."`)";
// Implode the values
$setVals = "('".implode("','",$arrVals)."')";
// Save to database
mysqli_query($con, "INSERT INTO quotation_terms {$setCols} values {$setVals}");
}
}