...

View Full Version : Problem with add a file detail to database



davincith
09-22-2007, 12:06 PM
I write a asp code to find files in the server directory as user type in the html page.when it find out ,it show file list that match value's user typing with "open file" button.If user click the button,it will add data about the opening file to database and show a hyperlink of that file
I have a problem at this point.I want to add the data of opeing only one file that user click but asp think that I want to open every files in the server directory.It will add the data of opening files in every to database.

this my code and my page.Please edit my code.I have to sent it on tuesday.I don't know to solve it anymore.Help!!!


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
Dim strPath
Dim objFSO
Dim objFolder
Dim strQuery


strPath = "C:\upload\"


strQuery = Request.QueryString("filesearch")
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strPath)


%>
<p>
Contents of <strong><%= strPath %></strong> that match your query:
</p>

<table border="5" bordercolor="green" cellspacing="0" cellpadding="2">
<tr bgcolor="#006600">
<td><font color="#FFFFFF"><strong>File&nbsp;Name:</strong></font></td>
<td width="50%"></td>

</tr>
<%
For Each x In objFolder.Files
If InStr(1, x.Name, strQuery, vbTextCompare) <> 0 Then

%>

<tr bgcolor="#CCFFCC">
<td align="left" ><%= x.Name %></td>

<td><form action="openfilesv.asp" method="post"><input type="submit" name=openf value="Open it"></form>
<% if request.form("openf")<>"" then
Set Objdb = Server.CreateObject("ADODB.Connection")
Objdb.Open "provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=c:\inetpub\wwwroot\mywebproject\fileinfo.mdb;"
Set RS = Server.CreateObject("ADODB.Recordset")
SQL = "SELECT * FROM [FileInfo]"
RS.Open SQL,Objdb,1,3
RS.Addnew
RS.Fields("Access Date")= Now
RS.Fields("File Name")=x.Name & "be opened"
RS.Update
RS.Close
Objdb.Close
Set RS=Nothing
Set Objdb=Nothing%>

<a href="<%= strPath & x.Name %>"><%= x.Name %></a>
<% end if%></td>

</tr></table>

<%
End If
Next
Set objItem = Nothing
Set objFolder = Nothing
Set objFSO = Nothing
set opf=nothing
set objfile=nothing
set objFSO=nothing

%>
</table>

I give user type file name.In this picture,I type hello


http://img216.imageshack.us/img216/7996/88101899au7.th.jpg (http://img216.imageshack.us/my.php?image=88101899au7.jpg)
It shows result


http://img212.imageshack.us/img212/4521/83110706ef4.th.jpg (http://img212.imageshack.us/my.php?image=83110706ef4.jpg)

I click only the button that is right of "hello.txt".It shows this


http://img216.imageshack.us/img216/6099/32099936ya2.th.jpg (http://img216.imageshack.us/my.php?image=32099936ya2.jpg)

this picture is my database after I click to open only "hello.txt"


http://img166.imageshack.us/img166/6721/88354076hv8.th.jpg (http://img166.imageshack.us/my.php?image=88354076hv8.jpg)

Daemonspyre
09-26-2007, 02:45 PM
I see your issue. You are using a SELECT * FROM [FileInfo] to get the records, but you need to use a WHERE statement to alter the name.

Also, you need to make sure that your form is not posting to every file.

Right now, you are saying in your code:


If my form is not empty, for every file that I have change the features.

You want it to say:


If my form is not empty, check my files for the one that I want, change its features, and leave the rest alone.

Here's what you need to do in order to fix this:

1) Set a hidden field with either your File Name OR your File's ID number.

2) ON Form Post, compare the above hidden field to the files in your database.

3) When you find a match, alter the Access Date and the File Name. Basically, your SQL statement should say SELECT * FROM [FileInfo] WHERE <hidden_field> = <hidden_field_value>


Couple quick notes:

1) NEVER, ever, unless absolutely necessary, use SELECT *. Always type in the fields you want to see into your SELECT statement.

2) Even in Access, spaces in Field Names is a big no-no. Change your field names to Access_Date and File_Name.

3) NOW is a function. It should be NOW(). Just because it works does not mean that you should be using it incorrectly.

HTH!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum