这是我的代码:
[G_TEST_CC] = {COST_CENTER};
[G_TEST_PLANT] = {PLANT};
header("Location: ../83211_TEST_PM_MONTH/83211_TEST_PM_MONTH.php");
$MONTH = array();
$MONTH[ 1 ] = 'JAN';
$MONTH[ 2 ] = 'FEB';
$MONTH[ 3 ] = 'MAR';
$MONTH[ 4 ] = 'APR';
$MONTH[ 5 ] = 'MAY';
$MONTH[ 6 ] = 'JUN';
$MONTH[ 7 ] = 'JUL';
$MONTH[ 8 ] = 'AUG';
$MONTH[ 9 ] = 'SEP';
$MONTH[ 10 ] = 'OCT';
$MONTH[ 11 ] = 'NOV';
$MONTH[ 12 ] = 'DEC';
for($i = 1; $i<=12 ; $i++){
sc_lookup(dataset, "select max(ID) from CMS.SUM_TEST");
$MAXID = {dataset[0][0]};
$NEWMAXID = $MAXID + $i;
// SQL statement parameters
$insert_table = 'EXPENSE_TEST_PM'; // Table name
$insert_fields = array( // Field list, add as many as needed
'ID' => "'$NEWMAXID'",
'BUDGET_YEAR' => "'[GLOBAL_BUDGETYEAR]'",
'VERSION' => "'[GLOBAL_VERSION]'",
'PLANT' => "'[G_TEST_PLANT]'",
'COST_CENTER' => "'[G_TEST_CC]'",
'MONTH' => "'$MONTH[$i]'",
);
// Insert record
$insert_sql = 'INSERT INTO ' . $insert_table
. ' (' . implode(', ', array_keys($insert_fields)) . ')'
. ' VALUES (' . implode(', ', array_values($insert_fields)) . ')';
sc_exec_sql($insert_sql);
}
我想根据PLANT和COST CENTER添加许多记录,但即使我提交了不同的PLANT和COST CENTER,我也只有12条记录。
此图片可能有助于了解此问题:
答案 0 :(得分:0)
正如Dipanwita Kundu所说,你的循环将在12次迭代后停止。如果您想要更多插入,您可能需要在第一个插入内部添加一个或两个循环来获取您的值。 我还建议你只做一个插入请求,而不是迭代,并且不使用字符串连接。这样的事情:
// set fields
$fields = ['ID', 'BUDGET_YEAR', 'VERSION', 'PLANT', 'COST_CENTER', 'MONTH'];
// set max id
sc_lookup(dataset, "select max(ID) from CMS.SUM_TEST");
$id = {dataset[0][0]};
// set sql
$insert_table = 'EXPENSE_TEST_PM'; // Table name
$insert_sql = 'INSERT INTO ' . $insert_table . ' (' . implode(', ', $fields) . ') VALUES ';
for($i = 1; $i<=12 ; $i++){
// you need to add new loop(s) here to get every data from active month
// it depends on your variables format
// here I assume that your PLANT and COST_CENTER are indexed by month
// for (...) // loop for cost center
// for (...) loop for plant
// SQL statement parameters
$insert_sql .= '('
. $id
. '{GLOBAL_BUDGETYEAR[0][0]}'
. '{GLOBAL_VERSION[0][0]}'
. '{PLANT[' . $i . '][' . $plantIndex. ']})'
. '{COST_CENTER[' . $i . '][' . $costCenterIndex . ']})'
. $i
. '),';
$id++;
// }
// } // end new loop
}
// remove last ","
$insert_sql = trim($insert_sql, ',');
// Insert records
sc_exec_sql($insert_sql);