<!--#include file="../../conn.asp"-->
<!--#include file="../inc/setup.asp"-->
<!--#include file="../inc/const.asp"-->
<!--#include file="../inc/check.asp"-->
<%
Admin_header
'=====================================================================
' 软件名称：新云网站管理系统
' 当前版本：NewAsp Content Management System Version 4.0
' 文件名称：admin_replace.asp
' 更新日期：2008-05-16
' 官方网站：新云网络(www.NewAsp.net www.NewAsp.cn) QQ：94022511
'=====================================================================
' Copyright 2003-2008 NewAsp.net - All Rights Reserved.
' newasp is a trademark of NewAsp.net
'=====================================================================
%>
<table class="table1" cellspacing="1" cellpadding="3" align="center" border="0">
	<tr>
		<td class="tableline linetitle" width="200" align="left">数据批量替换</td>
		<td class="tableline" width="*" align="right"><a href="admin_replace.asp">批量替换</a>
			 - <a href="admin_setting.asp">基本设置</a>
			 - <a href="admin_setting.asp?action=upload">上传设置</a>
			 - <a href="admin_setting.asp?action=plus">联盟插件设置</a>
			 - <a href="admin_setting.asp?action=editkey">内容关键字管理</a>
			 - <a href="admin_lockip.asp">IP限制管理</a>
			 - <a href="admin_badword.asp">过滤字符管理</a>
		</td>
	</tr>
</table>
<%
Dim Action
If Not ChkAdmin("BatchReplace") Then
	Call Transfer_error()
End If
Action = LCase(Request("action"))
Select Case Trim(Action)
	Case "replace"
		Call ReplaceString()
	Case "search"
		Call TableColumn()
	Case "table"
		Call Tabletop()
	Case Else
		Call showmain()
End Select
If FoundErr = True Then
	ReturnError(ErrMsg)
End If
Admin_footer
SaveLogInfo(AdminName)
NewAsp.PageEnd

Sub showmain()
%>
<form name="myform" action="?action=table" method="post">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
	<th>数据库批量替换管理——选择数据表名</th>
</tr>
<tr>
	<td align="center" class="tablerow2"><b>请选择要替换的数据表名：</b>
		<select name="TableName">
	<%
	Dim Rs
	Set Rs = Conn.openSchema(28)
	While Not Rs.EOF
		Response.Write ("<option value=""" & Rs(2) & """>" & Rs(2) & "</option>")
		Rs.movenext
	Wend
	%>
		</select>
		<input type="submit" name="submit_button" value="下一步" class="button">
	</td>
</tr>
<tr>
	<td align="center" class="tablerow1">请选择要替换的数据表</td>
</tr>
</table>
</form>
<%
End Sub

Sub Tabletop()
	Dim Rs,SQL,haveid,i,j
	Session("TableName") = NewAsp.checkStr(Trim(Request.Form("TableName")))
%>
<form name="myform" action="?action=search" method="post">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
	<th>数据库批量替换管理——选择字段名输入查找内容</th>
</tr>
<tr>
	<td align="center" class="tablerow1"><b>要替换的数据表名：</b>
		<select name="TableName">
			<option value="<%=Session("TableName")%>"><%=Session("TableName")%></option>
		</select>
	</td>
</tr>
<tr>
	<td align="center" class="tablerow2"><b>要替换的字段名：</b>
		<select name="ColumnName">
	<%
	haveid = 0
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT * FROM [" & Session("TableName") & "] WHERE 1<>1"
	Rs.Open SQL, Conn, 1, 1
	j = Rs.Fields.Count
	Session("NC_PRIMARY") = Rs.Fields(0).Name
	For i = 0 To (j - 1)
		Response.Write ("<option value='" & Rs.Fields(i).Name & "'>" & Rs.Fields(i).Name & "</option>")
	Next
	Rs.Close
	Set Rs = Nothing
	%>
		</select>
	</td>
</tr>
<tr>
	<td align="center" class="tablerow1"><b>字段名中包含的字符：</b>
	<input name="oldString" type="text" size="45"></td>
</tr>
<tr>
	<td align="center" class="tablerow2">
		<input type="button" name="go_button" onclick="javascript:history.go(-1)" value="返回上一页" class="button">
		<input type="submit" name="submit_button" value="开始查找" class="button">
	</td>
</tr>
<tr>
	<td align="center" class="tablerow1">请选择要替换的数据表</td>
</tr>
</table>
</form>
<%
End Sub

Sub TableColumn()
	On Error Resume Next
	If NewAsp.checkStr(Trim(Request.Form("oldString")))="" Then Response.Redirect ("admin_replace.asp")
	Dim Rs,SQL
	Session("ColumnName") = NewAsp.checkStr(Trim(Request.Form("ColumnName")))
%>
<form name="myform" action="?action=replace" method="post">
<table border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
<tr>
	<th>数据库批量替换管理——替换</th>
</tr>
<tr>
	<td align="center" class="tablerow1">
	<%
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	SQL = "SELECT COUNT(" & Session("NC_PRIMARY") & ") FROM " & Session("TableName") & " WHERE " & Session("ColumnName") & " like '%" & NewAsp.checkStr(Trim(Request.Form("oldString"))) & "%'"
	Rs.Open SQL, Conn, 1, 1
	If Not (Rs.EOF And Rs.bof) Then
		Session("oldString") = NewAsp.checkStr(Trim(Request.Form("oldString")))
		Response.Write ("本次搜索找到了 <b>" & Rs(0) & "</b> 个相关字符串。")
		Response.Write ("<a href=""javascript:history.go(-1)"">返回重新查找</a>")
	Else
		Response.Write ("没有找到相关字符串，<a href=""javascript:history.go(-1)"">返回重新查找</a>")
	End If
	Rs.Close
	Set Rs = Nothing
	%>
	</td>
</tr>
<tr>
	<td align="center" class="tablerow2"><b>将字符：</b>
		<input disabled name="oldString" type="text" size="45" value="<%=Trim(Request.Form("oldString"))%>">
	</td>
</tr>
<tr>
	<td align="center" class="tablerow1"><b>替换成：</b>
	<input name="newString" type="text" size="45" value=""></td>
</tr>
<tr>
	<td align="center" class="tablerow2">
		<input type="button" name="go_button" onclick="javascript:history.go(-1)" value="返回上一页" class="button">
		<input type="submit" name="submit_button" value="开始替换" class="button">
	</td>
</tr>
</table>
</form>
<%
End Sub

Sub ReplaceString()
	Dim Rs,SQL,i
	Dim oldString,newString
	Dim TableName,ColumnName
	Dim RepString,uprs,id

	oldString = NewAsp.checkStr(Trim(Session("oldString")))
	newString = NewAsp.checkStr(Trim(Request.Form("newString")))
	TableName = NewAsp.checkStr(Trim(Session("TableName")))
	ColumnName = NewAsp.checkStr(Trim(Session("ColumnName")))
	id = Trim(Session("NC_PRIMARY"))
	Set Rs=NewAsp.CreateAXObject("ADODB.Recordset")
	Set uprs=NewAsp.CreateAXObject("ADODB.Recordset")
	i = 0
	SQL = "SELECT " & id & "," & Session("ColumnName") & " FROM " & Session("TableName") & " WHERE " & Session("ColumnName") & " like '%" & Trim(Session("oldString")) & "%'"
	Rs.Open SQL, Conn, 1, 1
	If Not (Rs.EOF And Rs.BOF) Then
		'i = Rs.recordcount
		Do While Not Rs.EOF
			RepString = Replace(Rs(1), "" & oldString & "", "" & newString & "")
			SQL = "SELECT * FROM " & TableName & " WHERE " & id & "=" & Rs(0)
			uprs.Open SQL, Conn, 1, 3
			uprs(ColumnName) = RepString
			uprs.Update
			uprs.Close
			Rs.movenext
			i = i + 1
		Loop
	End If
	Rs.Close
	Set uprs = Nothing
	Set Rs = Nothing
	Succeed("<li>批量替换操作成功，共更新了 " & i & " 条信息！</li>")
End Sub

%>