如何使用两个insert查询循环checkbox中的两个值

gc0ot86w  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(341)

我有两个复选框,比如

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

我想用insert查询循环第一个值(名称),并在一个var中获取第二个值(成本)的总和,以将其插入另一个insert查询。
我可以把它炸了

$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."'
                    )";
}                       
}
kninwzqo

kninwzqo1#

使用数组时 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

另一个把戏,只是得到总 `cost` 从复选框值

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

现场演示

相关问题