PDA

View Full Version : Database record overwritten by latest entry


ooiooipig
12-07-2002, 01:40 AM
Hi Ppl, can i know y issit that when i enter new record into the databse, the last one is always overwritten by the latest entry??

secondly, i've a registration form and i've made a selection option for users to choose their secret question, how come all the other informations are passed into the database but the chosen secret question is not??

can anyone pls give me advice??

thanks.

Mhtml
12-07-2002, 01:51 AM
I think some source code is in order...it is a lot easier to examin the code to make a diagnosis rather than just guesssing what it could be... :D

ooiooipig
12-07-2002, 02:19 AM
<u>AddUser.asp</u>


<!--#include file = "adoObjects.asp"-->
<%
Dim rsUsers
Set rsUsers = Server.CreateObject("ADODB.Recordset")
rsUsers.Open "Person", objConn, adOpenForwardOnly, adLockOptimistic, adCmdTable

If Session("PersonID") <> "" Then 'currently logged-on user
rsUsers.Filter = "PersonID = '" & Session("PersonID") & "'"
Else 'new session
rsUsers.Filter = "UserID = '" & Request.Form("UserID") & "' " &_
"AND Passwd = '" & Request.Form("Passwd") & "' "

If rsUsers.EOF Then 'User not found
rsUsers.AddNew 'add a new record
' Else
' UserID and Password matched with DB records
' In this case we'll allow this to update user's personal details

End If
End If

rsUsers("PersonName") = Request.Form("PersonName")
rsUsers("UserID") = Request.Form("UserID")
rsUsers("Passwd") = Request.Form("Passwd")
rsUsers("SecretQns") = Request.Form("SecretQns")
rsUsers("SecretAns") = Request.Form("SecretAns")
rsUsers("School") = Request.Form("School")
rsUsers("Contact") = Request.Form("Contact")
rsUsers("Email") = Request.Form("Email")

rsUsers("LastLogin") = Now
rsUsers.Update 'Update DB

Dim strName, strvalue 'create session variables
For each strField in rsUsers.Fields
strName = strField.Name
strValue = strField.value
Session(strName) = strValue
Next
Session("blnValidUser") = True
'Declare that current user is validated
Response.Redirect "MenuForRegisteredUsers.asp"
%>

____________________________________________________
____________________________________________________
____________________________________________________
<u>Register.asp</u>


<!--#include file = "adoObjects.asp"-->

<%
Dim rsUsers, userid
Set rsUsers = Server.CreateObject("ADODB.Recordset")
rsUsers.Open "Person", objConn

userid = Session("UserID")
%>

<BASEFONT face="Comic Sans MS" COLOR="#000000">
<html>
<head>
<script language="JavaScript">
<!--

// function to validate form, checking error inputs by user
function VerifyData(f)
{
var i;
var contact_no = f.Contact.value;
var person_name = f.PersonName.value;
var email = f.Email.value;
var UserID = f.UserID.value;
var testid2;
var UserID3 = f.UserIDs.value
var passwd = f.Passwd.value;
var passwd2 = f.Passwd2.value;
var SecretQns;
var SecretAns = f.SecretAns.value;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

for(i=0;i<f.UserID2.length;i++)
{
var testid = f.UserID2[i].value;
if (UserID3 == testid)
testid2 = 0;
else if(UserID == testid)
testid2 = 1;
}
if(person_name == "")
alert("Key in PersonName");
else if(isNaN(contact_no) == true || contact_no.charAt(0)!= 6 && contact_no.charAt(0)!= 9 )
alert("Key in Valid Contact");
else if(email== "")
alert("Key in Email");
else if (filter.test(email) != true)
alert("Please input a valid email address!");
else if(UserID == "")
alert("Key in UserID");
else if(passwd == "" || passwd2 == "")
alert("Key in Password");
else if(passwd != passwd2)
alert("Your passwords do not match - please re-enter");
else if(SecretAns == "")
alert("Please choose your secret question and enter your secret answer.");
else if(testid2 ==1)
alert("Sorry, This UserId had been used, Please enter another UserId");
else
{
var confirm2 = confirm ("Are you sure you wish to register?")
if (confirm2==true)
f.submit();
else
alert("please enter your correct particulars...")

}
}
-->
</script>

<title>Registration Page for staff</title>

<body bgcolor="#CCCCFF" background="../images/paper_b003.gif">
<center>

<%
If Request("Update") = "True" Then
Response.Write "<h1>E-BookStore<br></h1><h2>Update Staff User Registration</h2>"
Else
Response.Write "<h1>E-BookStore<br></h1><h2>New Staff User Registration</h2>"
End If
%>
</center>
<div align="center">
<p align="center">
<%
If Request("Update") = "True" Then
Response.Write "Please change your registration information as listed below<p>"
Else If Request("NotFound") = "True" Then
Response.Write "<I>We were unable to locate your information." & "Please take the time to register again</i><p>"
Else
Response.Write "(If you're already registered with us, " & " then click the 'Login' link below)<p>"
End If
End If

%>
<%Response.Write "In order to use these services,"%>
<br>
<%Response.Write "Please take a few minutes to complete the form below."%>
<br>
<%Response.Write "Once you have done that,you will have full access to the system"%>
<form name="frmUser" method="post" action="AddUser.asp">
<div align="center">
<table width="70%" border="0">
<tr>
<td width="22%">Staff Name :</td>
<td width="51%"><input name="PersonName" type="text" value="<%If Request("Update")="True" Then Response.Write (Session("PersonName")) Else Response.Write ""%>" size="40" maxlength="30"></td>
<td width="27%">&nbsp;</td>
</tr>
<tr>
<td>School:</td>
<td><select name="School" value="<%If Request("Update")="True" Then Response.Write (Session("School")) Else Response.Write ""%>">
<option>Corp Dept</option>
<option>SBM</option>
<option>SEG(M)</option>
<option>SEG(E)</option>
<option>SHS</option>
<option>SCL</option>
<option>SIT</option>
<option>SDN</option>
<option>SIT(EI)</option>
<option>CPTC</option>
<option>SIT(Info Comm)</option>
</select> </td>
<td></td>
</tr>
<tr>
<td>Contact No.:</td>
<td><input name="Contact" type="text" value="<%If Request("Update")="True" Then Response.Write (Session("Contact")) Else Response.Write ""%>" size="8" maxlength="8">
(eg. 61234567 or 97654321)</td>
<td></td>
</tr>
<tr>
<td>Email Address:</td>
<td><input name="Email" type="text" value="<%If Request("Update")="True" Then Response.Write (Session("Email")) Else Response.Write ""%>" size="30" maxlength="30"></td>
<td></td>
</tr>
<tr>
<td>Login UserID:</td>
<td><input name="UserID" type="text" value="<%If Request("Update")="True" Then Response.Write (Session("UserID")) Else Response.Write ""%>" size="15" maxlength="12">
(Max 12 characters) #</td>
<td></td>
</tr>
<tr>
<td>Login Password:</td>
<td><input name="Passwd" type="password" value="" size="15" maxlength="10">
(Max 10 characters) #
<%Do until rsUsers.EOF%> <input type="hidden" name="UserID2" value="<%=rsUsers("UserID")%>"></td>
<%rsUsers.MoveNext%>
<%loop%>
<td><%If Request("Update")="True" Then Response.Write "Please re-enter your password" End If%></td>
<input type="hidden" name="UserIDs" value="<%=userid%>">
</tr>
<tr>
<td>Verify Password:</td>
<td><input name="Passwd2" type="password" size="15" maxlength="10"></td>
<td></td>
</tr>
<tr>
<td>Secret question:</td>
<td><select name:"SecretQns" value="<%If Request("Update")="True" Then Response.Write (Session("SecretQns")) Else Response.Write ""%>">
<option>Favourite pet's name?</option>
<option>Favourite movie?</option>
<option>Anniversary [dd/mm/yyyy]?</option>
<option>Spouse's name?</option>
<option>Child's name?</option>
<option>Favourite artiste?</option>
<option>Favourite sports?</option>
<option>Favourite food?</option>
<option>Where you stay?</option>
</select> </td>
<td></td>
</tr>
<tr>
<td>Secret Answer:</td>
<td><input name="SecretAns" type="text" value="<%If Request("Update")="True" Then Response.Write (Session("SecretAns")) Else Response.Write ""%>" size="20" maxlength=""></td>
<td></td>
</tr>
</table>
<br>
<table width="75%" border="0">
<tr>
<td height="31">* Please enter your secret question and answer so we can help you retrieve password. ,</td>
</tr>
<tr>
<td height="31">* Please give your mobile phone as contact no. ,</td>
</tr>
<tr>
<td>so that the bookstore staff can contact you for your collection.
</td>
</tr>
<tr>
<td>If you have changed your contact no. ,please remember to edit it.</td>
</tr>
<tr>
<td> <p align="center">Thank you.</p></td>
</tr>
</table>
<p align="center">
<input type="button" name="Submit" value="Submit Registration" onClick="VerifyData(frmUser)">
<input name="Reset" type="reset" id="Reset" value="Reset">
</p>
</div>
<p>

<p><img src="../images/lines_blue_078.gif" width="572" height="10"></p>
<div align="center">
<p><b><i><strong><em>You are now currently in the Registration Page</em></strong></i></b></p>
</div>
</form>
</div>
</body>
</html>

Morgoth
12-07-2002, 04:58 AM
Originally posted by ooiooipig
Hi Ppl, can i know y issit that when i enter new record into the databse, the last one is always overwritten by the latest entry??

secondly, i've a registration form and i've made a selection option for users to choose their secret question, how come all the other informations are passed into the database but the chosen secret question is not??

can anyone pls give me advice??

thanks.


Before I go testing out the source code myself, I would like you to remember, the only way it is possible for the last entry to be deleted and other one take it's place is if you updated the last record. With the new record.

So go through your code and debug it. Find out what information is going where, and why. Then ask yourself if that is correct, if not then fix it, if it's ok, follow that information to see if it doesn't change and gets put in the place where it should!

If I have problems like these, what I do is rewrite the code from scratch. I first do it part by part. First make code to add a new record ot the database, then I make sure it's bug free
Next I will add onto it. I will make it so your able to view that record in a table.
Thrid I will make it so you have update only one piece of information
Last I make it so you have a choice to update any piece of information depending on what record is selected.

When you have all that solved, you should add on the parts that you requir, such as Members only, Restricted access to only the Members records, Double checking for Members loged in, etc...

You can do alot with ASP, or it's always good to be able to READ your own code for later use and make it so it's upgradeable for some information that might be needed later or information you have yet to find out, later in life!


I will get back to you on what your problem is when I get a working copy running for myself.
(That's how I like to debug other peoples code, it picks out plain errors and allows me to read it ;) )

Morgoth
12-07-2002, 05:42 AM
In Register.asp I see some stuff that could help you out later when you program.

You Request("Update") twice here, when you could just use one (I did some changes to make it look nice ;) ).

Dim StrSUR
If Request("Update") = "True" Then
StrSUR = "Update Your Staff User Registration"
Else
StrSUR = "Create New Staff User Registration"
End If
%>
<h1>E-BookStore<br></h1><h2><%=StrSUR%></h2>
</center>
<div align="center">
<p align="center">Please LCase(StrSUR) information as listed below<p>
<%
If Request("NotFound") = "True" Then
Response.Write "<I>We were unable to locate your information." & "Please take the time to register again</i><p>"
Else
Response.Write "(If you're already registered with us, " & " then click the 'Login' link below)<p>"
End If


Next is:
If Request("Update")="True" Then Response.Write (Session("BLAH")) Else Response.Write ""
You don't need Else Response.Write "", you can delete that.
Or even better would be doing this (because if they are creating a new record they wont have anything in the session.) (put this at the top with userid = Session("UserID")):

StrEmail = Session("Email")
StrContact = Session("Contact")
StrSchool = Session("School")
StrPersonName = Session("PersonName")
StrSecretQns = Session("SecretQns")
StrSecretAns = Session("SecretAns")

Then make all the values for the text boxes = The strings it gives you, and if the session is empty, the value will be too.


I think I found your problem though....
Read next Reply.

Morgoth
12-07-2002, 06:41 AM
In AddUser.asp there is this:

If Session("PersonID") <> "" Then 'currently logged-on user
rsUsers.Filter = "PersonID = '" & Session("PersonID") & "'"
Else 'new session
rsUsers.Filter = "UserID = '" & Request.Form("UserID") & "' " &_
"AND Passwd = '" & Request.Form("Passwd") & "' "

If rsUsers.EOF Then 'User not found
rsUsers.AddNew 'add a new record
' Else
' UserID and Password matched with DB records
' In this case we'll allow this to update user's personal details

End If
End If


Then that means a new record should of been made!

Now you have this:

rsUsers("LastLogin") = Now
rsUsers.Update 'Update DB


This updates the new record, or the record that was edited.

Now what I want you to do is DEBUG this.
Meaning, I want you to make sure that top if statement gets run through fine if the user is and isn't logged in.
Make sure your getting your session veriables writen!

Now if that's correct, and the if statement is working for you, then I want you to go into your (assuming) access database and make sure your fields (in Design View) are not set as Required to Yes, make sure they are No.
This is because I think you are unable to create a New Record without having information to put into it while it's creating itself.
But I am not sure if that's the reason in the end, I have never done the method you use, but I might learn how to use it because it looks faster.

Tell me what happends, dude.

ooiooipig
12-09-2002, 05:03 AM
the database will still continue to overwrite the last entry. only after 15mins or so then it will add in new records upon registration and i've tried the ways u've taught me but there'r errors, so i change back to the original one. the project i'm doing now is to enhance other people's project, so i got problems with it. moreover, my asp coding is very weak, so gotta ask help from u all. sorry for the trouble.

Morgoth
12-09-2002, 06:07 AM
Oh, I see, Ok..

If your weak in ASP, then tutorials on using SQL should help you.

They way you are doing should work if you added a few things, maybe, but I am not familiar with the syntax.

I do, however, understand how ot use SQL, and if you change your method to SQL, most of us will be able to help you.

I like the way your method works though, I am going to try something like that later, and see what I get, but It might work better if you go with SQL.

http://aspin.com

Look up how to update, delete, add, retreive and such, this place will help you and give you many tutorials.

ooiooipig
12-09-2002, 11:46 AM
well, if sql is a better way, then i see if i can switch it to the sql method. hopefully can get some help here... if u trying this program, let me know if u succeed ok? maybe i can get some reference from you. thanks!!

Morgoth
12-09-2002, 01:22 PM
It shouldn't be to hard to switch to SQL, and of course we all will be here if you need any help.

ooiooipig
12-12-2002, 07:42 AM
still cannot change to sql

Morgoth
12-12-2002, 01:21 PM
Is it only becuase you don't know how or is it because you don't think it's possible? You will have to rewrite most of your ASP code for that one page. I am sure if you find out the syntax for SQL you'll get it. Ask questions!

ooiooipig
12-13-2002, 08:38 AM
i tried reading up some books n references for SQL but dont understand. maybe is because my programming is not good all along. the project that i'm doing now is carried on from the previous person, i got to enhance it. so actually i dont know which are the files that are related. only 2 or 3 simple files that i know are related, others i dont know.

whammy
12-14-2002, 12:29 AM
Hopefully the posts above haven't confused you too much, ASP is pretty easy once you mess around with it a bit... I would suggest getting "Beginning ASP 3.0" by Wrox.com - it was a pretty good book to learn from.

Doing something as simple as NOT overwriting an existing record is as easy as this:

MYQUERY = "SELECT * FROM mytablename WHERE SomeUniqueFieldHere = '" & SomeUniqueValue & "'"
Set rs = Conn.Execute(MYQUERY)

If NOT rs.EOF
' Don't INSERT a record - it already exists!!! However, in some cases you might want to UPDATE a record here.
Else
' INSERT a record!
End If

ooiooipig
12-14-2002, 02:45 AM
well.... ok i go n try putting in those sql queries... thanx!!