cfml插入DB记录

在demo2.cfm文件中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<cfform action="demolget.cfm" method="post">
   <table>
    <tr>
     <td>姓名:<cfinput type="text" required="yes" name="userName" /></td>
     <td>密码:<cfinput type="password" required="yes" name="userPwd" /></td>
    </tr>
    <tr>
     <td>出生日期:<cfinput type="datefield" required="yes" validate="Date" name="userBirthday" /></td>
     <td>男:<cfinput type="radio" name="userSex" required="yes" value="1" checked/>
     女:<cfinput type="radio" name="userSex" required="yes" value="0" /></td>
    </tr>
    <tr>
     <td>邮箱:<cfinput type="text" required="yes" name="userMail" /></td>
     <td>备注:<cfinput type="text" required="yes" name="userRemark" /></td>
    </tr>
    <tr>
     <td><cfinput type="submit" Name="insert" value="提交" /></td>
     <td><cfinput type="reset" name="reset" value="重置" /></td>
    </tr>
   </table>
 </cfform>

</body>

</html>

 

在demolget.cfm文件中

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
 <cfif isdefined("form.username")>
 
   <cfif #ParameterExists(Form.insert)# or isdefined("form.insert")>
    <cfif Not #LEN(Form.userName)# OR Not #LEN(Form.userPwd)# OR Not #LEN(Form.userSex)# OR Not #LEN(Form.userBirthday)#
     OR Not #LEN(Form.userMail)# OR Not #LEN(Form.userRemark)#>
     <cflocation url="demo2.cfm" />
    </cfif>
    <cfquery name="CFTESTDB" datasource="tangfuqiang">
     select * from userInfo where user_Name='#Form.userName#'
    </cfquery>
    <!---<cfdump var="#CFTESTDB#"> --->
     
    <cfif #CFTESTDB.recordCount# Gt 0>
     <cfoutput><center>员工#Form.userName#已经存在了,请重新输入!</center></cfoutput>
     
    <cfelse>
    <cfquery name="CFTESTDB" datasource="tangfuqiang">
    insert into userInfo(user_Name,user_Pwd,user_Birthday,user_Sex,user_Mail,user_Remark)
    values('#Form.userName#','#Form.userPwd#','#Form.userBirthday#',#Form.userSex#,'#Form.userMail#','#Form.userRemark#')
    </cfquery>
    
    </cfif>  
   </cfif>
  </cfif>
<body>
 <cfquery datasource="tangfuqiang" name="CFTESTDB">
   Select * from userInfo
  </cfquery>
  
  <!--- 建立一个HTML表格以显示Query --->
  <Table cellpadding = 1 cellspacing = 1>
   <TR>
    <TD colspan = 2 bgColor = f0f0f0><B>编号</B></TD>
    <TD bgColor = f0f0f0><B>姓名</B></TD>
    <TD bgColor = f0f0f0><B>密码</B></TD>
    <TD bgColor = f0f0f0><B>性别</B></TD>
    <TD bgColor = f0f0f0><B>邮箱</B></TD>
    <TD bgColor = f0f0f0><B>出生日期</B></TD>
    <TD bgColor = f0f0f0><B>备注</B></TD>
   </TR>
   
   <!--- 输出Query并定义startrow及maxrows参数值. 使用Query变量
   CurrentCount去追踪要显示的资料列. --->
   <!---获取总的记录数--->
   <cfset usercount=CFTESTDB.RecordCount />
   <CFOUTPUT Query="CFTESTDB">
    <TR>
      <TD valign = top><font size = "-1">#user_Id#</font></TD>
      <TD valign = top><font size = "-1">#user_Name#</font></TD>
      <TD valign = top><font size = "-1">#user_Pwd#</font></TD>
      <!--- 把从DB查询出来的性别转化为男、女--->
      <cfif #user_Sex# eq 1>
        <TD valign = top><font size = "-1">男</font></TD>
      <cfelse>
        <TD valign = top><font size = "-1">女</font></TD>
      </cfif>
      <TD valign = top><font size = "-1">#user_Mail#</font></TD>
      <TD valign = top><font size = "-1">#user_Birthday#</font></TD>
      <TD valign = top><font size = "-1">#user_Remark#</font></TD>
    </TR>
   </CFOUTPUT>
  </Table>
</body>
</html>

 

你可能感兴趣的:(cfml插入DB记录)