gpt4 book ai didi

php - 多种形式插入查询mysql

转载 作者:行者123 更新时间:2023-11-29 22:23:15 25 4
gpt4 key购买 nike

有谁知道如何在循环中格式化这个数组,以便它将在单个查询中但使用 php 以循环方式插入数据库中。

顺便说一句,我正在制作一个多重表单。

这是该字段的数组格式,这是我的表单,看起来像 attendance

Array
(
[sup_payroll_type] => Array
(
[0] => 1
[1] => 2
[2] => 1
)

[sup_month] => Array
(
[0] => January
[1] => January
[2] => May
)

[sup_year] => Array
(
[0] => 2015
[1] => 2015
[2] => 2015
)

[sup_late_days] => Array
(
[0] => 1
[1] => 1
[2] => 2
)

[sup_absent_days] => Array
(
[0] => 2
[1] => 1
[2] => 0
)

[sup_id] => Array
(
[0] => 1
[1] => 5
[2] => 6
)

[sup_emp_id] => Array
(
[0] => 24
[1] => 24
[2] => 24
)

[save_edit_satt] => Array
(
[0] => SAVE
)

)

如有任何帮助,我们将不胜感激。

最佳答案

请尝试以下循环:-

$insertQuery = 'INSERT INTO attendence (sup_payroll_type, sup_month, sup_year, 
sup_late_days, sup_absent_days, sup_id, sup_emp_id,
save_edit_satt) VALUES';
foreach($attendenceArray AS $key => $value){
$insertQuery .= '("'.$value['sup_payroll_type'].'","'.$value['sup_month'].'",
"'.$value['sup_year'].'","'.$value['sup_late_days'].'",
"'.$value['sup_absent_days'].'","'.$value['sup_id'].'",
"'.$value['sup_emp_id'].'","'.$value['save_edit_satt'].'"),';
}
$insertQuery = rtrim($insertQuery,',');

$insertQuery 包含您的插入查询。 $attendenceArray 包含您的示例数组。

如果您使用 mysqli,则使用 mysqli_query($mysqli,$insertQuery) 来执行您的查询。 $mysqli 是连接对象。

关于php - 多种形式插入查询mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30521026/

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