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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reload adds new record to db

Status
Not open for further replies.

Telsa

Programmer
Jun 20, 2000
393
US
I'm trying to build this internal site and the user has a form to fill out. When they click submit, it adds to the info to the database and then shows on the screen what was entered into the database.

However, since my database add and update code is on the same response page, everytime a user clicks the refresh button, it adds a new record with the same data to the database. How do I prevent that. Obviously I'm in the learning phase of ASP but really want to make this work.

Thanks! Mary :)

Rule 1: Don't sweat the small stuff.
Rule 2: EVERYTHING is small stuff!! X-)
 
You don't necessarily refresh the browser manually. You can simply add this code in the beginning of the page that has the form and updated data of the db table.

<%@Language=VBScript%>
<%Option Explicit%>
<%
Response.Expires = 60
Response.Expiresabsolute = Now() - 1
Response.AddHeader &quot;pragma&quot;,&quot;no-cache&quot;
Response.AddHeader &quot;cache-control&quot;,&quot;private&quot;
Response.CacheControl = &quot;no-cache&quot;
%>

Use response.redirect &quot;your page file name where it has the form and updated db data&quot;
after your codes updating the database.
This should resolve your data re-posting issue.

Regards
 
Oops... guess I didn't make myself very clear. I'm trying to prevent duplicate entries into the database if the user happens to click the refresh button on their browser.

What happens now is it keeps adding a new record everytime the button is clicked. I don't want it to add more than the one record that it does when the page opens. Mary :)

Rule 1: Don't sweat the small stuff.
Rule 2: EVERYTHING is small stuff!! X-)
 
Mary, oops, I see what you mean now. You can do a check first before INSERT a new record.

Dim strSQL, objRS, oConn, checkusername

connection string here.....

strSQL=&quot;SELECT COUNT(username) AS something FROM aTable WHERE username='&quot;
strSQL=strSQL & request(&quot;username&quot;) & &quot;'&quot;
Set objRS=Server.CreateObject(&quot;ADODB.Recordset&quot;)
objRS.Open strSQL,oConn,,adLockOptimistic,adCmdText
checkusername=objRS(&quot;something&quot;)

If checkusername=0 then
Put your INSERT codes here
Else
response.write &quot;Data has already been updated....&quot;
End If

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top