gpt4 book ai didi

php - 如何使用两个插入查询循环复选框中的两个值

转载 作者:行者123 更新时间:2023-11-29 17:00:18 25 4
gpt4 key购买 nike

我有一个带有两个值的复选框,例如

<input type="checkbox" name="analysis[]" value="'.$rows['name'].'_'.$rows['cost'].'">

我想使用插入查询循环第一个值(名称),并获取一个变量中第二个值(成本)的总和,以将其插入到另一个插入查询中。

我可以爆炸它

$analysis = explode("_", $_REQUEST['analysis']);
$name = $analysis[0];
$cost = $analysis[1];

但是循环第一个值和第二个值对我来说很复杂

我的完整 php 代码来了解我想要什么

$allAnalysis = $_REQUEST['analysis_type'];
$totalCost = 0;
foreach($allAnalysis as $k=>$analysis)
{
$analysis = explode("_", $analysis);
//$name = $analysis[0];
$cost = $analysis[1];
$totalCost += $cost;
}

$payment_option = $_REQUEST['payment_option'];

$amount_paid = $_REQUEST['amount_paid'];
if (!$amount_paid){$amount_paid = '0.00';}

$amount_remaining = $analysis_cost - $amount_paid ;
if ($amount_remaining == '0'){$amount_remaining = '0.00';}

$analysis_due = date('Y-m-d H:i:s', strtotime($_REQUEST['analysis_due']));

// analysis invoice number
$invoice_num = mysql_query(" SELECT max(invoice_id) FROM invoice ");
$invoice_num = mysql_fetch_array($invoice_num) + '1';
$invoice_num = 'H'.date('Y').date('m').str_pad($invoice_num,3,'0',STR_PAD_LEFT);

$invoice_insert = "INSERT INTO invoice (
invoice_id ,
analysis_cost ,
payment_option ,
amount_paid ,
amount_remaining ,
analysis_date ,
analysis_due ,
invoice_num ,
userid
) VALUES (
NULL ,
'".$totalCost."' ,
'".$payment_option."' ,
'".$amount_paid."' ,
'".$amount_remaining."' ,
NOW() ,
'".$analysis_due."' ,
'".$invoice_num."' ,
'".$_SESSION['userid']."'
)";

if (mysql_query($invoice_insert) === TRUE)
{
$invoice_id = " SELECT LAST_INSERT_ID() " ;
foreach($allAnalysis as $k=>$analysis)
{
$analysis = explode("_", $analysis);
$analysis_type = $analysis[0];
$analysis_inser = "INSERT INTO analysis (
analysis_id ,
horse_id ,
analysis_type ,
invoice_id
) VALUES (
NULL ,
'".$_GET['horse']."' ,
'".$analysis_type."' ,
'".$invoice_id."'
)";
}
}

最佳答案

当您使用数组analysis[]时,请使用任何循环,例如foreach

$allAnalysis = $_REQUEST['analysis'] ?? [];
$totalCost = 0;
foreach($allAnalysis as $k=>$analysis){
$analysis = explode("_", $analysis);
$name = $analysis[0];
$cost = $analysis[1];
$totalCost += $cost;
//Your DB stuff
}
// Use $totalCost for another purpose

从复选框值中获取总成本的另一个技巧

$arr = ["abc_111","abc_222","abc_333"];
$arrStr = explode(",",strrev(join(",",$arr)));
print_r(array_sum($arrStr));

Live Demo

关于php - 如何使用两个插入查询循环复选框中的两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52307515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com