| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5609 人关注过本帖
标题:[求助]"删除所选(复选框)"时怎样才能同时删除数据库记录及相关文件 ...
只看楼主 加入收藏
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 
这是filedel.asp的代码。
<!--#include file="conn.asp"-->
<%
On Error Resume Next
function DelFile(fPath,fName)
set fso=server.createobject("Scripting.filesystemobject")
if fso.fileexists(server.mappath(""&fPath&"/"&fName))=true then
fso.deletefile(server.mappath(""&fPath&"/"&fName))
End if
End Function
function DelAryFile(fAry,fPath)
Dim i
For i=0 To Ubound(fAry)
DelFile fPath,fAry(i)
Next
End Function
function DelDBFile(wStr,con)
con.execute "delete from [file] where "&wStr
end function
function getSqlQueryStr(Field,idStr)
Dim reStr
reStr=Field&"="&Replace(idStr,"|"," Or "&Field&"=")
getSqlQueryStr=reStr
End function
Dim id,filename,idAry,fAry,QueStr
id=trim(request("id"))
filename=trim(request("filename"))
if id <> "" or filename <> "" then
id=Mid(id,1,len(id)-1)
filename=Mid(filename,1,len(filename)-1)
idAry=Split(id,"|")
fAry=Split(filename,"|")
if InStr(id,"|")=0 Or InStr(filename,"|")=0 Then
DelFile SavePath,replace(request("filename"),"|","")
DelDBFile "id="&replace(request("id"),"|",""),conn
else
QueStr=getSqlQueryStr("id",id)
DelDBFile QueStr,conn
DelAryFile fAry,SavePath
end if
IF Err.Number<>0 Then
Response.write "程序出现错误,可能提交了非法参数或数据"
Else
Response.Redirect("show.asp")
End IF
Else
Response.write "没有接收到任何参数及数据,提交数据错误"
End IF
%>
修改完成后的filedel.asp的代码。
2007-10-04 10:25
thbwn
Rank: 1
等 级:新手上路
帖 子:235
专家分:0
注 册:2007-10-2
收藏
得分:0 
没事的,版主,我这都感动不已呢,在哪投票,我投你一票!!支持你.

          人人为我,我为人人!
2007-10-04 10:29
thbwn
Rank: 1
等 级:新手上路
帖 子:235
专家分:0
注 册:2007-10-2
收藏
得分:0 
楼主总结:


1、yms123版主,很有责任心,可以说是急网友之所急,不厌其烦,问题终于得到解决。
2、成功解决问题的代码最终是:
show.asp代码:
<%@ codepage=936 %>
<!--#include file="conn.asp"-->
<%
dim i,intPage,page,pre,last,filepath
set rs = server.CreateObject("adodb.recordset")
sql="select * from file order by ID"
rs.PageSize = 2 //(HacKpp:这里设定每页显示的记录数
rs.CursorLocation = 3
rs.Open sql,conn,0,2,1 //(HacKpp:'这里执行你查询SQL并获得结果记录集
pre = true
last = true
page = trim(Request.QueryString("page"))

if len(page) = 0 then
intpage = 1
pre = false
else
if cint(page) =< 1 then
intpage = 1
pre = false
else
if cint(page) >= rs.PageCount then
intpage = rs.PageCount
last = false
else
intpage = cint(page)
end if
end if
end if
if not rs.eof then
rs.AbsolutePage = intpage
end if
%>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>木目ASP文件上传工具(川江号子修改版)</title>
<link rel="stylesheet" href="Data\style.css">
</head>
<SCRIPT language=JavaScript type=text/javascript>
var idstr="";
var fNameStr="";
function Page_Submit()
{
if(document.kk.fID.length>1)
getSelIDFileName();
else
{
//alert(document.kk.fID.checked);
if(document.kk.fID.checked)
idstr=document.kk.fID.value;
fNameStr=document.kk.filename.value;
}
//alert(document.kk.fID.checked);
if(idstr=="")
{
alert('删除前请先选择要删除的内容');
return false;
}
if(confirm('删除后将不能恢复!您确定要删除吗?'))
{
self.location='filedel.asp?id='+idstr+'&filename='+fNameStr;
}
}
function CheckAll(form)
{
for (var i=0;i<form.elements.length;i++)
{
var e = form.elements[i];
if (e.name == 'fID')
{
e.checked = form.chkall.checked;
}
}
}
function getSelIDFileName()
{
var filenames=document.kk.filename;
var ids=document.kk.fID;
for(var i=0;i<ids.length;i++)
{
if(ids[i].checked)
{
idstr+=ids[i].value+"|";
fNameStr+=filenames[i].value+"|";
}
}
}
</script>
<body style="text-align: center">
<div align="center">
<table width="150" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#7B96BD" bordercolordark="#FFFFFF">
<tr>
<td height="22" align="center" bgcolor="#7C96B8"><font color="#FFFFFF">上传文件列表</font></td>
</tr>
</table>
<br>
<FORM name=kk action=del.asp method=post>
<table width="650" height="42" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#7C96B8" bordercolordark="#FFFFFF">
<TR align="center">
<td width="10%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">选择</font></td>
<td width="10%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">ID</font></td>
<td width="25%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">文件名</font></td>
<td width="20%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">文件大小</font></td>
<td width="25%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">上传时间</font></td>
</tr>
<%if rs.EOF or rs.BOF then
response.write "<TR><td colspan=10 height=20><div align=center>暂时还没有任何上传文件</div></td></TR>"
end if
%>
<%
for i=1 to rs.PageSize
if rs.EOF or rs.BOF then exit for
%>
<tr align=center>
<input type=hidden name=filename value=<%=rs("filename")%> />
<td width="10%" height="20" align="center"><input type=checkbox name=fID value=<%=rs("id")%> /></td>
<td width="10%" height="20" align="center"><%=rs("ID")%></td>
<td width="25%" align="center"><%=rs("fileName")%></td>
<td width="20%" align="center"><%=Split(Rs("contentlen"),".")(0)%>&nbsp;KB</td>
<td width="25%" align="center"><%=rs("uptime")%></td>
</tr>
<%
rs.movenext
next
%>
</table>
<p><DIV class=bottom><INPUT onclick=CheckAll(this.form) type=checkbox value=on
name=chkall> 全选&nbsp;&nbsp;<INPUT class=button onClick="Page_Submit();" type=button value=删除所选></DIV> </p></FORM>
<center>

<table width="650" border="0" cellpadding="3" cellspacing="1" borderColorLight=#808080 borderColorDark=#ffffff>
<tr>
<%if rs.pagecount > 0 then%>
<td width="13%" align="left">当前页<%=intpage%>/<%=rs.PageCount%></td>
<%else%>
<td width="40%" align="left">当前页0/0</td><%end if%>
<td width="47%" align="right"> <a href="Show.asp?page=1">首页</a>|
<%if pre then%>
<a href="Show.asp?page=<%=intpage -1%>">上页</a>| <%end if%>
<%if last then%>
<a href="Show.asp?page=<%=intpage +1%>">下页</a> |<%end if%>
<a href="Show.asp?page=<%=rs.PageCount%>">尾页</a>|转到第
<select name="sel_page" onChange="javascript:location=this.options[this.selectedIndex].value;">
<%
for i = 1 to rs.PageCount
if i = intpage then%>
<option value="Show.asp?page=<%=i%>" selected><%=i%></option>
<%else%>
<option value="Show.asp?page=<%=i%>"><%=i%></option>
<%
end if
next
%>
</select>页</font>
</td>
</tr>
</table>
<p>&nbsp;</p>
<p><a href="upload.asp" target="_self"><font color="#0000FF">[返回上传页]</font></a>  <a href="mdbzip.asp" target="_self"><font color="#0000FF">[压缩数据库]</font></a></p>
</center>
</div>
</body>

</html>



filedel.asp代码:

<!--#include file="conn.asp"-->
<%
On Error Resume Next
function DelFile(fPath,fName)
set fso=server.createobject("Scripting.filesystemobject")
if fso.fileexists(server.mappath(""&fPath&"/"&fName))=true then
fso.deletefile(server.mappath(""&fPath&"/"&fName))
End if
End Function
function DelAryFile(fAry,fPath)
Dim i
For i=0 To Ubound(fAry)
DelFile fPath,fAry(i)
Next
End Function
function DelDBFile(wStr,con)
con.execute "delete from [file] where "&wStr
end function
function getSqlQueryStr(Field,idStr)
Dim reStr
reStr=Field&"="&Replace(idStr,"|"," Or "&Field&"=")
getSqlQueryStr=reStr
End function
Dim id,filename,idAry,fAry,QueStr
id=trim(request("id"))
filename=trim(request("filename"))
if id <> "" or filename <> "" then
id=Mid(id,1,len(id)-1)
filename=Mid(filename,1,len(filename)-1)
idAry=Split(id,"|")
fAry=Split(filename,"|")
if InStr(id,"|")=0 Or InStr(filename,"|")=0 Then
DelFile SavePath,replace(request("filename"),"|","")
DelDBFile "id="&replace(request("id"),"|",""),conn
else
QueStr=getSqlQueryStr("id",id)
DelDBFile QueStr,conn
DelAryFile fAry,SavePath
end if
IF Err.Number<>0 Then
Response.write "程序出现错误,可能提交了非法参数或数据"
Else
Response.Redirect("show.asp")
End IF
Else
Response.write "没有接收到任何参数及数据,提交数据错误"
End IF
%>

3、再次对版主说声“谢谢”,我在别的论坛发出帖子,但只有在这里问题才在最短的时间里得到解决,我会常来的。

[此贴子已经被作者于2007-10-4 10:53:46编辑过]


          人人为我,我为人人!
2007-10-04 10:45
thbwn
Rank: 1
等 级:新手上路
帖 子:235
专家分:0
注 册:2007-10-2
收藏
得分:0 

不用filedel.asp!

通过show.asp就能实现这种效果,自己琢磨的,这个要比版主的简单,能看明白。
原理:不通过ID,而是通过文件名调用实现两种功能。

<%@ codepage=936 %>
<!--#include file="conn.asp"-->
<%
dim i,intPage,page,pre,last,filepath
set rs = server.CreateObject("adodb.recordset")
sql="select * from file order by ID"
rs.PageSize = 15 //(HacKpp:这里设定每页显示的记录数
rs.CursorLocation = 3
rs.Open sql,conn,0,2,1 //(HacKpp:'这里执行你查询SQL并获得结果记录集
pre = true
last = true
page = trim(Request.QueryString("page"))

if len(page) = 0 then
intpage = 1
pre = false
else
if cint(page) =< 1 then
intpage = 1
pre = false
else
if cint(page) >= rs.PageCount then
intpage = rs.PageCount
last = false
else
intpage = cint(page)
end if
end if
end if
if not rs.eof then
rs.AbsolutePage = intpage
end if
%>
<%
Select Case Request("Action")
Case"DelOptFile"
DelOptFile
end select
%>

<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>木目ASP文件上传工具(川江号子修改版)</title>
<link rel="stylesheet" href="Data\style.css">
</head>
<SCRIPT language=JavaScript type=text/javascript>
function checkclick(msg,url){if(confirm(msg))window.location.href=url;}
function openwin(url,w,h,s){window.open(url,'_blank','status=yes,scrollbars='+s+',top=20,left=110,width='+w+',height='+h);}
function CheckAll(form) {
for (var i=0;i<form.elements.length;i++) {
var e = form.elements[i];
if (e.name == 'fID'){
e.checked = form.chkall.checked;
}
}
}
</script>
<body style="text-align: center">
<div align="center">
<table width="150" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#7B96BD" bordercolordark="#FFFFFF">
<tr>
<td height="22" align="center" bgcolor="#7C96B8"><font color="#FFFFFF">上传文件列表</font></td>
</tr>
</table>
<br>
<form name='kk' method='POST' action='?Action=DelOptFile'>
<table width="650" height="42" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#7C96B8" bordercolordark="#FFFFFF">
<TR align="center">
<td width="10%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">选择</font></td>
<td width="10%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">ID</font></td>
<td width="25%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">文件名</font></td>
<td width="20%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">文件大小</font></td>
<td width="25%" height="20" bgcolor="#006666" align="center"><font color="#FFFFFF">上传时间</font></td>
</tr>
<%if rs.EOF or rs.BOF then
response.write "<TR><td colspan=10 height=20><div align=center>暂时还没有任何上传文件</div></td></TR>"
end if
%>
<%
for i=1 to rs.PageSize
if rs.EOF or rs.BOF then exit for
%>
<tr align=center>
<td width="10%" height="20" align="center"><input type=checkbox name=fID value=<%=rs("filename")%> /></td>
<td width="10%" height="20" align="center"><%=rs("ID")%></td>
<td width="25%" align="center"><%=rs("fileName")%></td>
<td width="20%" align="center"><%=Split(Rs("contentlen"),".")(0)%>&nbsp;KB</td>
<td width="25%" align="center"><%=rs("uptime")%></td>
</tr>
<%
rs.movenext
next
%>
</table>
<p><DIV class=bottom><input type=checkbox name=chkall value=on onClick="CheckAll(this.form)"> 全选&nbsp;&nbsp;<input class="button" value="删除所选" type="button" onclick="if(confirm('删除后将不能恢复!您确定要删除吗?'))form.submit()" /></div> </p></FORM>
<center>

<table width="650" border="0" cellpadding="3" cellspacing="1" borderColorLight=#808080 borderColorDark=#ffffff>
<tr>
<%if rs.pagecount > 0 then%>
<td width="13%" align="left">当前页<%=intpage%>/<%=rs.PageCount%></td>
<%else%>
<td width="40%" align="left">当前页0/0</td><%end if%>
<td width="47%" align="right"> <a href="Show.asp?page=1">首页</a>|
<%if pre then%>
<a href="Show.asp?page=<%=intpage -1%>">上页</a>| <%end if%>
<%if last then%>
<a href="Show.asp?page=<%=intpage +1%>">下页</a> |<%end if%>
<a href="Show.asp?page=<%=rs.PageCount%>">尾页</a>|转到第
<select name="sel_page" onChange="javascript:location=this.options[this.selectedIndex].value;">
<%
for i = 1 to rs.PageCount
if i = intpage then%>
<option value="Show.asp?page=<%=i%>" selected><%=i%></option>
<%else%>
<option value="Show.asp?page=<%=i%>"><%=i%></option>
<%
end if
next
%>
</select>页</font>
</td>
</tr>
</table>
<p>&nbsp;</p>
<p><a href="upload.asp" target="_self"><font color="#0000FF">[返回上传页]</font></a>  <a href="mdbzip.asp" target="_self"><font color="#0000FF">[压缩数据库]</font></a></p>
</center>
</div>
</body>
</html>

<%
'删除所选
Sub DelOptFile
Dim FileName,FSO,Folder,Files,Upname,Temp,i
On Error Resume Next
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If Err Then
Goback01()
err.Clear
Exit Sub
End If
FileName=Request("fID")
If FileName="" Then GoBack02():Exit Sub
Temp=Split(FileName,",")
For i=0 To uBound(Temp)
conn.execute("Delete * From file Where FileName='"&Trim(Temp(i))&"'")
Next
Set Folder=fso.Getfolder(server.MapPath(SavePath&"\"))
Set files=folder.files
For Each Upname In files
If instr(FileName,Upname.name)>0 then
FSO.DeleteFile(Server.MapPath(SavePath&"/"&upname.name))
End if
Next
Set Folder=nothing
Set Files=nothing
Set Fso=nothing
response.Redirect("show.asp")
End Sub
%>
<%
Sub GoBack01()
Response.Write"<script>alert('操作失败,空间不支持FOS文件读写!');location.replace('show.asp')</script>"
End Sub

Sub GoBack02()
Response.Write"<script>alert('没有选择文件,删除失败!');location.replace('show.asp')</script>"
End Sub
%>


[此贴子已经被作者于2007-10-4 15:59:37编辑过]


          人人为我,我为人人!
2007-10-04 15:23
yms123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:209
帖 子:12488
专家分:19042
注 册:2004-7-17
收藏
得分:0 
For i=0 To uBound(Temp)
conn.execute("Delete * From file Where FileName='"&Trim(Temp(i))&"'")
Next
Set Folder=fso.Getfolder(server.MapPath(SavePath&"\"))
Set files=folder.files
For Each Upname In files
If instr(FileName,Upname.name)>0 then
FSO.DeleteFile(Server.MapPath(SavePath&"/"&upname.name))
End if
Next
我写的那个只用了一个循环,数据库中的删除用了sql语句代替了循环。个人从执行效率考虑,少一个循环可以提高一些程序的执行效率。
2007-10-04 16:48
thbwn
Rank: 1
等 级:新手上路
帖 子:235
专家分:0
注 册:2007-10-2
收藏
得分:0 
支持版主,向你学习!!!

          人人为我,我为人人!
2007-10-04 20:59
快速回复:[求助]"删除所选(复选框)"时怎样才能同时删除数据库记录及相关文件 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027707 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved