1 回答
TA贡献1891条经验 获得超3个赞
我可以在您的示例中看到两种类型的查询。在这里共享单个服务类型插入和插入与总价格。希望这有帮助!!
<div class="container">
<form action="" method="post">
<input type="checkbox" name="services[]" value="oilchange" />oilchange
<input type="checkbox" name="services[]" value="acrepair" />acrepair
<input type="checkbox" name="services[]" value="tyrechange" />tyrechange
<br/>
<input type="submit" name="btnservice" value="Confirm Services">
</form> <?php
$serviceChargeArr = array(
'fullservice' => 5000,
'tyrebalance' => 4000,
'oilchange' => 3000,
'acrepair' => 2000,
'tyrechange' => 1000
);
$priceTotal = 0.0;
if (isset($_POST['btnservice'])) {
if (isset($_POST['services'])) {
$serviceArr = $_POST['services'];
$serviceArrCnt = count($serviceArr);
//QUERY 1 - insert each with respect to clicked checkboxes
for ($i = 0; $i < $serviceArrCnt; $i++) {
if ($eachPrice = $serviceChargeArr[$serviceArr[$i]]) {
$insertArr[] = "('$serviceArr[$i]', '$eachPrice')";
}
}
if (!empty($insertArr)) {//sigle insert query -each service and its price will be inserted
$query="insert into tblservices (service_type, amount)
values".implode(", ",$insertArr ).";";
echo $query."<br/>";
}//QUERY 1 ENDS
//QUERY 2 - insert single row with comma separated values and total price
for ($i = 0; $i < $serviceArrCnt; $i++) {
if ($eachPrice = $serviceChargeArr[$serviceArr[$i]]) {
$priceTotal += $eachPrice;
}
}
if (!empty($serviceArr)) {
$seviceList = implode(',', $serviceArr);
$query="insert into tblservices (service_type, amount)
values ('{$seviceList}' , '{$priceTotal}') ;";
echo $query."<br/>";
}//QUERY 2 ENDS
if($query) {
$run = mysqli_query($con, $query);
if ($run) {
echo "<br/> SUCCESS : inserted";
} else {
echo "<br/> ERROR : try again<br/>Mysql Error: ".$con->error;
}
}
}
} ?>
</div>
- 1 回答
- 0 关注
- 116 浏览
添加回答
举报