CFML操作DB的添、删、改、查

demolget.cfm文件.

此文件内的代码主要完成对userInfo的添、删、改操作.

<!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>
 <!---<cfoutput>#url.user_Id#   #url.param#</cfoutput>--->
 <!---  打印form表单元素 <cfdump var="#Form#"> --->
 
 <cfif #url.param# eq "update">
  <!--- update DB's userInfo table--->
  <cfif #ParameterExists(Form.update)# or isdefined("form.update")>
   <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="updateUserInfo.cfm" />
   </cfif>
   <cfquery name="CFTESTDB" datasource="tangfuqiang">
    update userInfo set user_Name='#Form.userName#',user_Pwd='#Form.userPwd#',user_Birthday='#Form.userBirthday#'
       ,user_Sex=#Form.userSex#,user_Mail='#Form.userMail#',user_Remark='#Form.userRemark#'             
       where user_Id=#url.user_Id#
   </cfquery>
  </cfif>
 <cfelseif #url.param# eq "insert">
  <!--- insert DB's userInfo table--->
  <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="insertUserInfo.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>
 <cfelse>
  <!--- Is userInfo record deleted exist in DB--->
  <cfquery name="CFTESTDB" datasource="tangfuqiang">
   select * from userInfo where user_Id=#url.param#
  </cfquery>
  <!---select DB's userInfo return result zero to prove no recored in DB--->
  <cfif #CFTESTDB.recordCount# Lt 0>
   <cfoutput>删除的用户不存在</cfoutput>
  <cfelse>
   <cfquery name="CFTESTDB" datasource="tangfuqiang">
    delete userInfo where user_Id=#url.param#
   </cfquery>
  </cfif> 
 </cfif>
 
 
 <cfinclude template="demol3.cfm">
</body>
</html>

 

 

 

 

 

 

updateUserInfo.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>
 <cfquery datasource="tangfuqiang" name="CFTESTDB">
  Select * from userInfo where user_Id='#url.user_Id#'
 </cfquery>
   
 <!--- 输出Query并定义startrow及maxrows参数值. 使用Query变量
  CurrentCount去追踪要显示的资料列 --->
 <CFOUTPUT Query="CFTESTDB">
  <center>
  <cfform action="demolget.cfm?param=update&user_Id=#url.user_Id#" method="post">
    <table>
     <tr>
      <td>姓名:<cfinput type="text" required="yes" name="userName" value="#user_Name#" /></td>
      <td>密码:<cfinput type="text" required="yes" name="userPwd" value="#user_Pwd#" /></td>
     </tr>
     <tr>
      <td>出生日期:<cfinput type="text" required="yes" validate="Date" name="userBirthday" value="#user_Birthday#" /></td>
      <td>
       <cfif #user_Sex# eq 1>
        男:<cfinput type="radio" name="userSex" required="yes" value="1" checked="yes" />
       <cfelse>
        女:<cfinput type="radio" name="userSex" required="yes" value="0" checked="yes" />
       </cfif>
      </td>
     </tr>
     <tr>
      <td>邮箱:<cfinput type="text" required="yes" name="userMail" value="#user_Mail#" /></td>
      <td>备注:<cfinput type="text" required="yes" name="userRemark" value="#user_Remark#" /></td>
     </tr>
     <tr>
      <td><cfinput type="submit" Name="update" value="提交" /></td>
      <td><cfinput type="reset" name="reset" value="重置" /></td>
     </tr>
    </table>
  </cfform>
  <center>
 </CFOUTPUT>
 
</body>
</html>

 

 

 

 

insertUserInfo.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>
 <center>
  <cfform action="demolget.cfm?param=insert" 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>
 <center>
</body>
</html>

 

 

 

selectUserInfo.cfm文件:

 

<!--- 让coldfusion强制转换成utf-8编码格式--->
<cfprocessingdirective pageencoding="utf-8">

<!--- select code start --->
<cfquery datasource="tangfuqiang" name="CFTESTDB">
 Select * from userInfo
</cfquery>

<cfif isdefined("Form.showCount")>
 <cfoutput>总数据:#Form.showCount#</cfoutput>
 <cfabort>
</cfif>

<!--- <cfoutput>总数据:#Form.totalCount#  每页显示:#Form.showCount#  当前第#Nowindex#页</cfoutput>--->

<center>
<!--- 建立一个HTML表格以显示Query --->
<Table cellpadding = 0 cellspacing = 0 border="1" bgcolor="#33FF99" width="800" height="50">
 <TR>
  <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>
  <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>
   <TD valign = top><font size = "-1">
    <a href="updateUserInfo.cfm?user_Id=#user_Id#">编辑</a>
    <a href="demolget.cfm?param=#user_Id#">删除</a>
    <a href="insertUserInfo.cfm">添加</a>
   </font></TD>
  </TR>
 </CFOUTPUT>
 <!--- 分页查询--->
 <TR>
  <td colspan="8">
   <cfform action="" method="post">
     共<cfinput type="text" value="#CFTESTDB.recordCount#" name="totalCount" size="1" readonly />条数据
     当前第<cfinput type="text" value="1" name="Nowindex" size="1" onBlur="" readonly />页
     每页显示<cfinput type="text" name="showCount" value="5" size="1" />
     <cfinput type="button" name="lastPage" value="上一页" />
     <cfinput type="button" name="nextPage" value="上一页" />
     请输入第<cfinput type="text" name="pageCount" size="1" value="0" />页
     <cfinput type="submit" value="查询" name="pageQuery" />  
   </cfform>
  </td>  
 </TR>
</Table>
</center>
<!--- select code end --->

 

 

 

 

 

demol3.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>
 <cfinclude template="selectUserInfo.cfm">
</body>
</html>

你可能感兴趣的:(CFML操作DB的添、删、改、查)