|
 |
asp_databases thread: Display, Update from/into a ACCESS DB in same ASP page
Message #1 by Gui You <guiyou01@y...> on Tue, 4 Feb 2003 10:01:03 -0800 (PST)
|
|
Hi all,
Any idea for displaying the record and updating the record from/into a ACCESS DB at same ASP page?
Thanks.
YoYo
---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
Message #2 by "Paul Douglas" <pdouglas@t...> on Wed, 5 Feb 2003 00:41:55
|
|
If I understand correctly, I usually do this sort of thing by utilising
the Querystring or by using a hidden Form element and having the Form
submit to itself by using the special Request.ServerVariables
("SCRIPT_NAME") property. I'm assuming you're using a form to allow the
user to interact before performing an update.
eg. on the one ASP page I would have something like :
<%
if Request.Querystring("update")=1 then
' insert code here to perform UPDATE to DB
else
' insert code here to perform SELECT
Response.Write("<form name='TestForm' method='POST' action='" &
Request.ServerVariables("SCRIPT_NAME") & "?update=1'">
' Output your SELECT results and form
Response.Write("</form>")
end if
%>
When first run the "else" part of the page would be loaded (ie. SELECT
part) as the "update" will not exist in the Querystring. But when the
form is submitted "update=1" is appended to the Querystring and the form
is submitted to itself. The if Request.Querystring("update")=1 test
evaluates to true this time and executes the first part of the if
statement (the UPDATE part).
You could use a hidden form element named "update" with a value of "1"
instead of the Querystring or even use GET method on the form rather than
POST to "hide" the querystring but to see what's happening and debug when
you run a page like this I find this the easier method....
Hope that makes sense
Cheers
Paul
Message #3 by Gui You <guiyou01@y...> on Wed, 5 Feb 2003 10:59:40 -0800 (PST)
|
|
Paul, Thank you for kindly helping me.
I have a multiple data in the database. Each data contains a few info like person, address, phone, email, etc. They should be
displayed in a ASP page, and will be allowed to update at same or other page. So my codes likes following:
- Display page with a action button = Update
?Getting the existing data from the database
connectivity here (Conn as a object of ADODB.Connection, and RS as a object of ADODB.Recordset)
SQL = ? Select * From table Where id = ? & GetThisId
RowNum = 1
While Not RS.EOF
Response.Write ?<table>?
Response.Write ?<form name=??frmname? & RowNum & ?? action=??updatepage.asp?Id=? & GetThisId & ??
method=??post??>?
Response.Write ?<tr><td>Person<input type=text name=ThisPerson value=?? & RS(?Person?) &
??></td></tr>?
Response.Write ?<tr><td>Address<input type=text name=ThisAddress value=?? & RS(?Address?) &
??></td></tr>?
Response.Write ?<tr><td>Phone<input type=text name=ThisPhone value=?? & RS(?Phone?) &
??></td></tr>?
Response.Write ?<tr><td>EMail<input type=text name=ThisEmail value=?? & RS(?Email?) &
??></td></tr>?
Response.Write ?<tr><td>Person<input type=hidden name=ThisId value=?? & RS(?Id?) &
??></td></tr>?
Response.Write ?<tr><td><input type=button name=action value=?? Update ?? onClick=??document.frmname ? & RowNum
& ?.submit();??></td></tr>?
Or Response.Write ?<tr><td><input type=submit name=action value=?? Update ??
onClick=??parent.location=?updatepage.asp?Id=? & GetThisId & ???></td></tr>?
RowNum = RowNum + 1
Wend
- Update page
Connectivity here
GetThisId = Request.QueryString(?Id?)
RS(?Person?) = trim(request.form(?ThisPerson?))
RS(?Address?) = trim(request.form(?ThisAddress?))
RS(?Phone?) = trim(request.form(?ThisPhone?))
RS(?Email?) = trim(request.form(?ThisEmail?))
SQL = ?Update table Set Person = RS(?Person?), ??, Email = RS(?Email?)
? Where Id = ? & GetThisId
Conn.Execute (SQL)
But My problem is : if the database has only a person info, Updatepage works fine; but since there are should have more than one
data in the database, I got the trouble by updating. I feel bad about that. Please continue to help me on this. Thanks.
YoYo
Paul Douglas <pdouglas@t...> wrote:
If I understand correctly, I usually do this sort of thing by utilising
the Querystring or by using a hidden Form element and having the Form
submit to itself by using the special Request.ServerVariables
("SCRIPT_NAME") property. I'm assuming you're using a form to allow the
user to interact before performing an update.
eg. on the one ASP page I would have something like :
if Request.Querystring("update")=1 then
' insert code here to perform UPDATE to DB
else
' insert code here to perform SELECT
Response.Write("Request.ServerVariables("SCRIPT_NAME") & "?update=1'">
' Output your SELECT results and form
Response.Write("")
end if
%>
When first run the "else" part of the page would be loaded (ie. SELECT
part) as the "update" will not exist in the Querystring. But when the
form is submitted "update=1" is appended to the Querystring and the form
is submitted to itself. The if Request.Querystring("update")=1 test
evaluates to true this time and executes the first part of the if
statement (the UPDATE part).
You could use a hidden form element named "update" with a value of "1"
instead of the Querystring or even use GET method on the form rather than
POST to "hide" the querystring but to see what's happening and debug when
you run a page like this I find this the easier method....
Hope that makes sense
Cheers
Paul
---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
Message #4 by "Paul Douglas" <pdouglas@t...> on Thu, 6 Feb 2003 00:12:12
|
|
Yoyo,
Is the GetThisId a unique ID for each Person in the Database? If so wont
that mean that the line:
SQL = ? Select * From table Where id = ? & GetThisId
will only ever return one record anyway?
Aside from that and again if I'm on the right track.....It seems that in
this page you are creating multiple unique forms by using GetThisId to
create forms called something like frmname1, frmname2, frmname3
etc....where each of these forms has fields for Person, Address, Phone etc
and it's own SUBMIT button. Therefore when you click on any one of these
SUBMIT buttons it will only pass details of the current form (the form
that the SUBMIT button belongs to) which contains just the one record to
the Update Page.
One approach might be to only have the ONE form but to use a variable to
make each field in that form unique and to include a HIDDEN field for each
record that contains each records ID.
Response.Write ?<table>?
Response.Write ?<form name=??frmname? & RowNum & ??
action=??updatepage.asp?? method=??post??>?
RowNum = 1
While Not RS.EOF
Response.Write ?<input type=HIDDEN name=UniqueID? & RowNum & ?? value=?? &
RS(?Person?) & ??>?
Response.Write ?<tr><td>Person<input type=text name=ThisPerson? & RowNum
& ?value=?? & RS(?Person?) & ??></td></tr>?
Response.Write ?<tr><td>Address<input type=text name=ThisAddress? & RowNum
& ?value=?? & RS(?Address?) & ??></td></tr>?
......
......
RowNum =RowNum +1
Wend
Response.Write ?<tr><td><input type=button name=action value=?? Update ??
onClick=??document.frmname ? & RowNum & ?.submit();??></td></tr>?
Response.Write ?</form></table>?
That way with only the one SUBMIT button ALL fields are passed on to
updatepage.asp for processing:
For i=1 to MaxNumRows
....
SQL = ?Update table Set Person = RS(?Person?), ??, Email = RS(?Email?) ?
Where Id = ? Request.Form("UniqueID" & i)
....
Next
So each time this FOR loop executes, the value (containing the ID for each
record) in UniqueID1, UniqueID2 etc is used to UPDATE the appropriate row
with the correct values for Person, Email etc....
Of course this might become unworkable if you expect to have hundreds of
records....
Hope that makes sense and is along the lines of what you were asking....
Message #5 by Gui You <guiyou01@y...> on Fri, 7 Feb 2003 05:44:04 -0800 (PST)
|
|
Hi Paul,
Thank you for your powerful note. Sorry for delaying reply. The GetThisId is really unique ID for each record. You know what I
solved this problem by using dynamic form and dynamic attributes of names.
<form name=frmname" & RSNum & " action=samepage.asp method=post>
<input type=text name=person" & RSNum & " value=" & RS(Person) & ">
.....
SQL = update table set Person = "' & Request("person" & RSNum & "") & '" where id=" & GetThisId
-YoYo
Paul Douglas <pdouglas@t...> wrote:Yoyo,
Is the GetThisId a unique ID for each Person in the Database? If so wont
that mean that the line:
SQL = ? Select * From table Where id = ? & GetThisId
will only ever return one record anyway?
Aside from that and again if I'm on the right track.....It seems that in
this page you are creating multiple unique forms by using GetThisId to
create forms called something like frmname1, frmname2, frmname3
etc....where each of these forms has fields for Person, Address, Phone etc
and it's own SUBMIT button. Therefore when you click on any one of these
SUBMIT buttons it will only pass details of the current form (the form
that the SUBMIT button belongs to) which contains just the one record to
the Update Page.
One approach might be to only have the ONE form but to use a variable to
make each field in that form unique and to include a HIDDEN field for each
record that contains each records ID.
Response.Write ??
Response.Write ?action=??updatepage.asp?? method=??post??>?
RowNum = 1
While Not RS.EOF
Response.Write ? [input] RS(?Person?) & ??>?
Response.Write ?Person [input] & ?value=?? & RS(?Person?) & ??>?
Response.Write ?Address [input] & ?value=?? & RS(?Address?) & ??>?
......
......
RowNum =RowNum +1
Wend
Response.Write ? [input] onClick=??document.frmname ? & RowNum & ?.submit();??>?
Response.Write ??
That way with only the one SUBMIT button ALL fields are passed on to
updatepage.asp for processing:
For i=1 to MaxNumRows
....
SQL = ?Update table Set Person = RS(?Person?), ??, Email = RS(?Email?) ?
Where Id = ? Request.Form("UniqueID" & i)
....
Next
So each time this FOR loop executes, the value (containing the ID for each
record) in UniqueID1, UniqueID2 etc is used to UPDATE the appropriate row
with the correct values for Person, Email etc....
Of course this might become unworkable if you expect to have hundreds of
records....
Hope that makes sense and is along the lines of what you were asking....
---------------------------------
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now
|
|
 |