如何将3行数据保存到MySQL数据库中

时间:2013-11-15 22:22:41

标签: mysql coldfusion coldfusion-9

我有一个“时间输入”表格,有三个可能的输入行。

  • 可以在一行,两行或全部三行输入时间..
  • 问题:如何构建我的INSERT查询语句以将值保存到数据库?
  • 使用ColdFusion 9 w / MySQL 5.5

以下代码已发布here,但我不确定如何应用于以下HTML:

具体来说,如何将“ mon1,tue1,wed1,... mon2,tue3,wed4等”值插入查询

<cfquery name="simpleInsert" datasource="mydatasource">
INSERT INTO tblTimeTracking (col1,col2,col3)
VALUES 
<cfloop from="1" to="5" index="i">
<cfif i GT 1>,</cfif>
(#i#,'somename','sometitle')
</cfloop>
</cfquery>

需要保存哪些数据的示例:

enter image description here

我的三行的HTML:

<tbody>
<tr>
  <th><select name="select-choice-1" id="select-choice-1" data-native-menu="false" data-theme="c" data-mini="true">
    <option value="Regular Hourly Rate" selected>Regular Hourly Rate</option>
    <option value="Paid Leave Time">Paid Leave Time</option>
    <option value="Bereavement">Bereavement</option>
    <option value="Holiday Pay">Holiday Pay</option>
    </select></th>
  <td><input type="number" pattern="[0-9]*" name="mon1" id="mon1" value="8" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="tue1" id="tue1" value="8" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="wed1" id="wed1" value="8" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="thu1" id="thu1" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="fri1" id="fri1" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sat1" id="sat1" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sun1" id="sun1" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="text" pattern="[0-9]*" name="total1" id="total1" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><label>
    <input name="desc1" type="text" id="desc1" size="15" maxlength="50" data-mini="true">
    </label></td>
</tr>
<tr>
  <th><select name="select-choice-2" id="select-choice-2" data-native-menu="false" data-theme="c" data-mini="true">
    <option value="Regular Hourly Rate">Regular Hourly Rate</option>
    <option value="Paid Leave Time" selected>Paid Leave Time</option>
    <option value="Bereavement">Bereavement</option>
    <option value="Holiday Pay">Holiday Pay</option>
    </select></th>
  <td><input type="number" pattern="[0-9]*" name="mon2" id="mon2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="tue2" id="tue2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="wed2" id="wed2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="thu2" id="thu2" value="8" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="fri2" id="fri2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sat2" id="sat2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sun2" id="sun2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="text" pattern="[0-9]*" name="total2" id="total2" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><label>
    <input name="desc2" type="text" id="desc2" size="15" maxlength="50" data-mini="true">
    </label></td>
</tr>
<tr>
  <th><select name="select-choice-3" id="select-choice-3" data-native-menu="false" data-theme="c" data-mini="true">
    <option value="Regular Hourly Rate">Regular Hourly Rate</option>
    <option value="Paid Leave Time">Paid Leave Time</option>
    <option value="Bereavement">Bereavement</option>
    <option value="Holiday Pay" selected>Holiday Pay</option>
    </select></th>
  <td><input type="number" pattern="[0-9]*" name="mon3" id="mon3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="tue3" id="tue3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="wed3" id="wed3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="thu3" id="thu3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="fri3" id="fri3" value="8" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sat3" id="sat3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="number" pattern="[0-9]*" name="sun3" id="sun3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><input type="text" pattern="[0-9]*" name="total3" id="total3" value="" size="10" maxlength="5" data-mini="true"></td>
  <td><label>
    <input name="desc3" type="text" id="desc3" size="15" maxlength="50" data-mini="true">
    </label></td>
</tr>
<tr>
  <th>&nbsp;</th>
  <td colspan="7"><div align="right">Totals</div></td>
  <td><input type="text" pattern="[0-9]*" name="total4" id="total4" value="" size="10" maxlength="5" data-mini="true"></td>
  <td>&nbsp;</td>
</tr>

我当前第一行的INSERT查询的一部分(工作正常,但希望使用上面的cfloop示例缩小它:

<cfquery datasource="care">   
INSERT INTO tbltimetracking (ttID, ttUser, ttCategory, ttHoursWorked, ttWorkDay,  
ttDescription, ttCreated)

VALUES 
<!---Monday Row 1--->
(<cfif IsDefined("FORM.ttID") AND #FORM.ttID# NEQ "">
<cfqueryparam value="#FORM.ttID#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.ttUser") AND #FORM.ttUser# NEQ "">
<cfqueryparam value="#FORM.ttUser#" cfsqltype="cf_sql_clob" maxlength="75">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.selectchoice1") AND #FORM.selectchoice1# NEQ "">
<cfqueryparam value="#FORM.selectchoice1#" cfsqltype="cf_sql_clob" maxlength="75">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.mon1") AND #FORM.mon1# NEQ "">
<cfqueryparam value="#FORM.mon1#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.monD") AND #FORM.monD# NEQ "">
<cfqueryparam value="#FORM.monD#" cfsqltype="cf_sql_timestamp">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.ttDescription") AND #FORM.ttDescription# NEQ "">
<cfqueryparam value="#FORM.ttDescription#" cfsqltype="cf_sql_clob" maxlength="55">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.ttCreated") AND #FORM.ttCreated# NEQ "">
<cfqueryparam value="#FORM.ttCreated#" cfsqltype="cf_sql_timestamp">
<cfelse>
NULL
</cfif>),
<!---Tuesday Row 1--->
(<cfif IsDefined("FORM.ttID") AND #FORM.ttID# NEQ "">
<cfqueryparam value="#FORM.ttID#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.ttUser") AND #FORM.ttUser# NEQ "">
<cfqueryparam value="#FORM.ttUser#" cfsqltype="cf_sql_clob" maxlength="75">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.selectchoice1") AND #FORM.selectchoice1# NEQ "">
<cfqueryparam value="#FORM.selectchoice1#" cfsqltype="cf_sql_clob" maxlength="75">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.tue1") AND #FORM.tue1# NEQ "">
<cfqueryparam value="#FORM.tue1#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.tueD") AND #FORM.tueD# NEQ "">
<cfqueryparam value="#FORM.tueD#" cfsqltype="cf_sql_timestamp">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.ttDescription") AND #FORM.ttDescription# NEQ "">
<cfqueryparam value="#FORM.ttDescription#" cfsqltype="cf_sql_clob" maxlength="55">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.ttCreated") AND #FORM.ttCreated# NEQ "">
<cfqueryparam value="#FORM.ttCreated#" cfsqltype="cf_sql_timestamp">
<cfelse>
NULL
</cfif>),
<!---Wednesday Row 1---> 
etc...
</cfquery>

0 个答案:

没有答案