gpt4 book ai didi

javascript - 迭代动态创建的表并将数据插入 SQL Server 表

转载 作者:行者123 更新时间:2023-11-28 20:35:42 25 4
gpt4 key购买 nike

我有一个 .html 表单动态创建的表,我想在 .cfm 表单中进行插入。我需要循环遍历动态创建的表的行并对 SQL Server 中的表执行 INSERT。此外,该表是用 JavaScript 创建的。谢谢。

<cfoutput>
<cfloop from="1" to="#ArrayLen(tblSample)#" index="i">
<cfquery name="AppendForm" datasource="TestSource">
INSERT INTO tblGrand
(GrandNum,
GrandName,
County,
VersionType,
VersionNum,
SectCode,
Comments,
Provider,
TypeID,
SubmitDate)
Select
<cfif isdefined("form.GrandNum")>
'#GrandNum#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.GrandNme")>
'#GrandNme#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.selCnty")>
'#selCnty#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.VersionType")>
'#VersionType#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.VersionNum")>
'#VersionNum#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.SectCode[i]")>
'#SectCode[i]#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.txtComments[i]")>
'#textComments[i]#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.txtProvider[i]")>
'#txtProvider[i]#',
<cfelse>
null,
</cfif>
<cfif isdefined("form.selType[i]")>
'#selType[i]#',
<cfelse>
null,
</cfif>
'#DateFormat(Now(),"yyyy-mm-dd") &" "& TimeFormat(Now(),"HH:mm:ss")#'
</cfquery>
</cfloop>
</cfoutput>

这是我用于创建表的 html 代码:

<script language="javascript" type="text/javascript">
function addRow() {

var tbl = document.getElementById('tblSample');
var lastRow = tbl.rows.length;
var iteration = lastRow;
var row = tbl.insertRow(lastRow);

// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(iteration-3);
cellLeft.appendChild(textNode);

// select cell
var cellRightSel = row.insertCell(1);
var sel = document.createElement('select');
sel.name = 'sectCode' + iteration;
sel.id = 'sectCode' + iteration;
sel.options[0] = new Option('---Any---', '0');
sel.options[1] = new Option('Level 0.5: test1, '1');
sel.options[2] = new Option('Level I: test2', '2');
sel.options[3] = new Option('Level I.D: test3', '3');
sel.options[4] = new Option('Level II.1: test4', '4');
sel.options[5] = new Option('Level II.5: test5', '5');
cellRightSel.appendChild(sel);

var cellRights = row.insertCell(2);
var els = document.createElement('input');
els.type = 'text';
els.name = 'txtComments' + iteration;
els.id = 'txtComments' + iteration;
els.size = 20;
cellRights.appendChild(els);

var cellRight = row.insertCell(3);
var el = document.createElement('input');
el.type = 'text';
el.name = 'txtProvider' + iteration;
el.id = 'txtProvider' + iteration;
el.size = 20;
cellRight.appendChild(el);

var cell7 = row.insertCell(8);
var sel5 = document.createElement('select');
sel5.name = 'selType' + iteration;
sel5.id = 'selType' + iteration;
sel5.options[0] = new Option('---Any---', '---Any---');
sel5.options[1] = new Option('Fees, 'Fees);
sel5.options[2] = new Option('Reimbursement', 'Reimbursement');
cell7.appendChild(sel5);
}

最佳答案

您需要使用VALUES插入数据库。您还应该使用 structKeyExists 而不是 isDefined,并且还应该使用 cfqueryparam。我也会调整你的变量的范围。

<cfloop from="1" to="#ArrayLen(tblSample)#" index="i">
<cfquery name="AppendForm" datasource="TestSource">
INSERT INTO tblGrand
(GrandNum,
GrandName,
County,
VersionType,
VersionNum,
SectCode,
Comments,
Provider,
TypeID,
SubmitDate)
VALUES (
<cfif structKeyExists(form,"GrandNum")>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.GrandNum#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"GrandNme")>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.GrandNme#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"selCnty")>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.selCnty#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"VersionType")>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.VersionType#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"VersionNum")>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form.VersionNum#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"SectCode"&i)>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form['SectCode'&i]#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"txtComments"&i)>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form['textComments'&i]#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"txtProvider"&i)>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form['txtProvider'&i]#">,
<cfelse>
NULL,
</cfif>
<cfif structKeyExists(form,"selType"&i)>
<cfqueryparam cfsqltype="cf_sql_varchar" value="#form['selType'&i]#">,
<cfelse>
NULL,
</cfif>
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#Now()#">)
</cfquery>
</cfloop>

关于javascript - 迭代动态创建的表并将数据插入 SQL Server 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15436110/

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