Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate that filename does not exist before submitting to DB

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi,

I ahve been able to create some pages to allow users to upload files to the server and also add the filename to a table in a DB however, I have one problem that perhaps someone could help me with.

When the user wants to upload a file the first thing they have to do is add the filename to the DB, the field in the DB is indexed to not allow duplicates, at the moment if a duplicate filename is submitted then all that happens is a error message on the web page.

Couls someone help me to be able to validate the DB on submit that the filename does not already exist and if it does then display a message to the user before the submit

Regards

Paul

 
Check for the file name before you do your insert statement. If there is an error, then give them an error message and process from there. Some psuedocode to help you along:
Code:
conn = [myConnectionObject]
rs = [recordset object]
strSQL = "SELECT [filename] FROM [myTable] WHERE [filename] = '" & theUserFileName & "'"
rs.open strSQL, conn
if rs.BOF and rs.EOF then
  response.write "Your filename already exists.  Please choose another." [COLOR=green]'From here, you can do whatever[/color]
else 
  [COLOR=green]'You can process your insert statement here.[/color]
end if

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Hi chopstik,

thanks for your speedy reply, I am trying to understand your reply, could you confirm that I am on the right track

1. I have a form that has the textfields in it one of which is "txtFileName"

2. the user types in the filename

3. I then use code similar to what you have shown above to query the table to see if the filename exists

4. If so then tell the user and ask them to change the filename

5. Otherwise submit the record to the DB

would the query be run when the user hits the submit button ??

Regards

Paul

 
That is my understanding, yes. You will test the value when you submit the page (as you must call back to the server in order to check the database table) and, if not a duplicate, then insert the value into the table. If it is a duplicate, simply send an error message to the user or do whatever else you want it to do.

Bear in mind that there may be other ways but this should get you going on the right path...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top