救急//添加数据function inserttable($tablename, $insertsqlarr, $returnid0, $replace false, $silen
救急
//添加数据
function inserttable($tablename, $insertsqlarr, $returnid=0, $replace = false, $silent=0) {
global $_SGLOBAL;
$insertkeysql = $insertvaluesql = $comma = '';
foreach ($insertsqlarr as $insert_key => $insert_value) {
$insertkeysql .= $comma.'`'.$insert_key.'`';
$insertvaluesql .= $comma.'\''.$insert_value.'\'';
$comma = ', ';
}
$method = $replace?'REPLACE':'INSERT';
$_SGLOBAL['db']->query($method.' INTO '.tname($tablename).' ('.$insertkeysql.') VALUES ('.$insertvaluesql.') ', $silent?'SILENT':'');
if($returnid && !$replace) {
return $_SGLOBAL['db']->insert_id();
}
}
这是我用PHP写的一个插入函数,能帮我改成coldfusion么$insertsqlarr是一个数组参数。明天我再加100分
[最优解释]没经过测试。
<cffunction name="inserttable">
<cfargument name="tablename">
<cfargument name="insertsqlarr" type="array">
<cfargument name="returnid" default="0">
<cfargument name="replace" default="0">
<cfargument name="silent" default="0">
<cfset var insertkeysql = "">
<cfset var insertvaluesql = "">
<cfset var comma = "">
<cfloop from="1" to="#arraylen(insertsqlarr)#" index="l_key">
<cfset insertkeysql = insertkeysql & comma & '`' & a[l_key][1] & '`'>
<cfset insertvaluesql = insertvaluesql & comma & "'" & a[l_key][2] & "'">
<cfset comma = ", ">
</cfloop>
<cfset var method = iif(replace eq 1, de("REPLACE"), de("INSERT"))>;
<cfset var qry_run = "">
<cfquery name="qry_run" datasource="your data source">
#method# into #tablename# (#insertkeysql#) values (#preservesinglequotes(insertvaluesql)#)
<cfif returnid eq 1 and replace eq 0>
;
select max(id) as maxid from #tablename#
</cfif>
</cfquery>
<cfif returnid eq 1 and replace eq 0>
<cfreturn qry_run.maxid>
<cfelse>
<cfreturn "">
</cfif>
</cffunction>
[其他解释]不是很懂,我帮你问下同事。。。
[其他解释]谢谢你,PHP函数应该不难,但是不知道怎么用coldfusion把他表现出来
[其他解释]UP
[其他解释]没经过测试。
<cffunction name="inserttable">
<cfargument name="tablename">
<cfargument name="insertsqlarr" type="array">
<cfargument name="returnid" default="0">
<cfargument name="replace" default="0">
<cfargument name="silent" default="0">
<cfset var insertkeysql = "">
<cfset var insertvaluesql = "">
<cfset var comma = "">
<cfloop from="1" to="#arraylen(insertsqlarr)#" index="l_key">
<cfset insertkeysql = insertkeysql & comma & '`' & a[l_key][1] & '`'>
<cfset insertvaluesql = insertvaluesql & comma & "'" & a[l_key][2] & "'">
<cfset comma = ", ">
</cfloop>
<cfset var method = iif(replace eq 1, de("REPLACE"), de("INSERT"))>;
<cfset var qry_run = "">
<cfquery name="qry_run" datasource="your data source">
#method# into #tablename# (#insertkeysql#) values (#preservesinglequotes(insertvaluesql)#)
<cfif returnid eq 1 and replace eq 0>
;
select max(id) as maxid from #tablename#
</cfif>
</cfquery>
<cfif returnid eq 1 and replace eq 0>
<cfreturn qry_run.maxid>
<cfelse>
<cfreturn "">
</cfif>
</cffunction>
[其他解释]好的我测试一下谢谢你
[其他解释]
function updatetable($tablename, $setsqlarr, $wheresqlarr, $silent=0) {
global $_SGLOBAL;
$setsql = $comma = '';
foreach ($setsqlarr as $set_key => $set_value) {
$setsql .= $comma.'`'.$set_key.'`'.'=\''.$set_value.'\'';
$comma = ', ';
}
$where = $comma = '';
if(empty($wheresqlarr)) {
$where = '1';
} elseif(is_array($wheresqlarr)) {
foreach ($wheresqlarr as $key => $value) {
$where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
$comma = ' AND ';
}
} else {
$where = $wheresqlarr;
}
$_SGLOBAL['db']->query('UPDATE '.tname($tablename).' SET '.$setsql.' WHERE '.$where, $silent?'SILENT':'');
}
最后帮我改一个谢谢了
[其他解释]<cffunction name="updatetable">
<cfargument name="tablename">
<cfargument name="setsqlarr" type="array">
<cfargument name="wheresqlarr">
<cfargument name="silent" default="0">
<cfset var setsql = "">
<cfset var comma = "">
<cfset var l_key = 0>
<cfloop from="1" to="#arraylen(setsqlarr)#" index="l_key">
<cfset setsql = setsql & comma & '`' & a[l_key][1] & '`'>
<cfset setsql = setsql & "='" & a[l_key][2] & "'">
<cfset comma = ", ">
</cfloop>
<cfset var where ="">
<cfset comma = "">
<cfif wheresqlarr eq "">
<cfset where = "1">
<cfelseif isarray(wheresqlarr)>
<cfloop from="1" to="#arraylen($wheresqlarr)#" index="l_key">
<cfset where = where & comma & '`' & a[l_key][1] & '`'>
<cfset where = where & "='" & a[l_key][2] & "'">
<cfset comma = " and ">
</cfloop>
<cfelse>
<cfset where = wheresqlarr>
</cfif>
<cfset var method = iif(replace eq 1, de("REPLACE"), de("INSERT"))>;
<cfset var qry_run = "">
<cfquery name="qry_run" datasource="your data source">
update #tablename# set #preservesinglequotes(setsql)# where #preservesinglequotes(where)#
</cfquery>
<cfreturn "">
</cffunction>
[其他解释]上面写多了一行。
<cffunction name="updatetable">
<cfargument name="tablename">
<cfargument name="setsqlarr" type="array">
<cfargument name="wheresqlarr">
<cfargument name="silent" default="0">
<cfset var setsql = "">
<cfset var comma = "">
<cfset var l_key = 0>
<cfloop from="1" to="#arraylen(setsqlarr)#" index="l_key">
<cfset setsql = setsql & comma & '`' & a[l_key][1] & '`'>
<cfset setsql = setsql & "='" & a[l_key][2] & "'">
<cfset comma = ", ">
</cfloop>
<cfset var where ="">
<cfset comma = "">
<cfif wheresqlarr eq "">
<cfset where = "1">
<cfelseif isarray(wheresqlarr)>
<cfloop from="1" to="#arraylen($wheresqlarr)#" index="l_key">
<cfset where = where & comma & '`' & a[l_key][1] & '`'>
<cfset where = where & "='" & a[l_key][2] & "'">
<cfset comma = " and ">
</cfloop>
<cfelse>
<cfset where = wheresqlarr>
</cfif>
<cfset var qry_run = "">
<cfquery name="qry_run" datasource="your data source">
update #tablename# set #preservesinglequotes(setsql)# where #preservesinglequotes(where)#
</cfquery>
<cfreturn "">
</cffunction>
[其他解释]
强啊,英雄贴