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

Can you use a form to write to a database? 2

Status
Not open for further replies.

JoeM6

Programmer
Aug 5, 2000
53
US
Can you use a form to write to a database? I know you can use a form to write to an .asp file, but i mean like a .mdb file.

JoeM6
JPMJR11@aol.com
 
That didn't totally make sense, but ok....

You can easily add information into your database useing data that was put in a form by a client / user on the web.

Create your form on one page, ex:


---page1.asp------------------------------------

<form method='post' action='page2.asp'>
Name:<input type='text' name='userName'>
Email:<input type='text' name='userEmail'>
<input type='submit' value='Submit'><input type='reset' value='Reset'>
</form>

------------------------------------------------

This will take the input by the client of user name and email address, and submit it to page2.asp. There you will request the information, then make a connection to the database and insert it, and then close your connection and clean up. Ex:

---page2.asp------------------------------------

<%
Dim username, useremail, sql, conn, dsn, rs
username = Request.Form(&quot;userName&quot;)
useremail = Request.Form(&quot;userEmail&quot;)
sql = &quot;INSERT INTO myTable ( name, email ) VALUES ( '&quot; & username & &quot;', '&quot; & useremail & &quot;' );&quot;

'connection
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
dsn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
dsn = dsn & &quot;DBQ=c:\InetPub\ conn.Open dsn
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open sql, conn, 3, 3
conn.Close
Set conn = nothing

response.redirect(&quot;page3.asp&quot;)

%>

----------------------------------------------------

page2.asp inserted the information into the database, and then forwarded you to page3.asp.

Hope this is what you were looking for and that it helped you out!
-Ovatvvon :-Q
 
Whoops, forgot I was corrected before...
What I gave you will work, however instead of useing a rs.Open sql, conn, 3, 3...since this is a command, it would be more correct to say somthing like,

conn.Execute(sql)


Hope I got that right!
But, the way I gave you first off will work just fine if you can't get this second way to work.

-Ovatvvon
 
ok, i have a little problem, it says page cannot be displayed. So far I have

<%@ Language=VBScript %>


<%
Dim username, password, sql, conn, dsn, rs
username = Request.Form(&quot;username&quot;)
password = Request.Form(&quot;password&quot;)
sql = &quot;INSERT INTO myTable ( username, password ) VALUES ( '&quot; & username & &quot;', '&quot; & password & &quot;' );&quot;

'connection
Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
dsn = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
dsn = dsn & &quot;DBQ=\gamew0rld\db\tblLogin.mdb;&quot;
conn.Open dsn
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
conn.Execute(sql)
conn.Close
Set conn = nothing

response.redirect(&quot;add.html&quot;)

%>



I am using brinkster, and it says that this code below is what i should use to connect to my database.


Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;\MemberName\db\dbname.mdb&quot;)


Is there anything that you can find that I did wrong? The only thing is, where it says myTable, does that have to be the name of the table? It is called tblLogin as well. I tried it like that too, but it still doesnt work

JoeM6
JPMJR11@aol.com
 
ok,
server.mappath() is used to trace the server's harddrive physical path up to the location of the file that contains the server.mappath() script, and will include in the layout anything you enter between (&quot; &quot;). forinstance, if brinksters layout for web stuff was c:\inetpub\ you can put server.mappath(&quot;\memberName\db\database.mdb&quot;) and it will map c:\inetpub\ for you and insert the text you put in (\memberName\db\database.mdb) after it.

But when you are altering your files on brinkster, aren't you already working in your &quot;memberName&quot; folder? so I wouldn't think you'd have to put the memberName inside the server.mappath script. But I also haven't worked with brinkster..that's just what makes sense to me. Try it both ways if you are having problems with it.

Here is code I came up with you, assuming you have a database named &quot;database.mdb&quot; and a table inside that db named &quot;tablename&quot; and fields inside the table named &quot;username&quot; and &quot;useremail&quot;.

PAGE 1 &quot;test.asp&quot;
-----------------------------------------------

<form method='post' action='test2.asp'>
Name:<input type='text' name='userName'><BR>
Email:<input type='text' name='userEmail'><BR>
<DD><input type='submit' value='Submit'><input type='reset' value='Reset'>
</form>

-----------------------------------------------



PAGE 2 &quot;test2.asp&quot;
-----------------------------------------------


<%@ Language=VBscript %>
<% Option Explicit %>
<%
Dim username, useremail, sql, conn, DSNtemp
username = Request.Form(&quot;userName&quot;)
useremail = Request.Form(&quot;userEmail&quot;)

sql = &quot;INSERT INTO tablename ( username, useremail ) VALUES ( '&quot; & username & &quot;', '&quot; & useremail & &quot;' );&quot;

Set conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
DSNtemp = &quot;DRIVER={Microsoft Access Driver (*.mdb)};&quot;
DSNtemp = DSNtemp & &quot;DBQ=&quot; & Server.MapPath(&quot;\memberName\db\database.mdb&quot;) & &quot;;&quot;
conn.Open DSNtemp
conn.Execute(sql)
conn.Close
Set conn = nothing


response.redirect(&quot;test.asp&quot;)

%>


-------------------------------------------------


so for testing, create a database named database, a table within that named tablename, and a pk field, a username field, and a useremail field. Put it in your memberName\db\ folder, and try it. If it doesn't work, try without memberName. If it still doesn't work, contact me again. I'll let you try it out on my server if you like, but see if you can get it to work on brinkster first.

Hope this helps!
-Ovatvvon :-Q
 
THANK YOU SO MUCH, IT WORKS!!! the only thing i didnt under stand was when you said pk field. Also, on the test.asp page. Do those have to be in the same order than on the database file?

JoeM6
JPMJR11@aol.com
 
PK = Primary Key
Is a autonumber field in your datbase. Not required, but good to have because if any time you want to make references to information in your database, somtimes information will be identical on the way down...like if you have a user firstname field and a last name field, and you might have two people signup that are named Jeff Smith. Well, with a pk field, one jeff smith would have an identity of 5 (the fifth person in the database) and the other might be 89 (the 89th person in the database). That way you can specify which one you want to deal with when manipulating records.

test.asp, when designing your form. It does not matter what order your elements are on the form page because all you are doing is passing that information to the next page (test2.asp) and requesting the information. On the next page, it doesn't matter which order you request them either, you are just gathering information.

It will matter though what order you write your sql statement in for inserting your info into the database...for example:

sql = &quot;INSERT INTO tablename ( username, useremail ) VALUES ( '&quot; & username & &quot;', '&quot; & useremail & &quot;' );&quot;

that is telling it to insert &quot; & username & &quot; into username, and &quot; & useremail & &quot; into useremail. The first field name you declare in the sql statement for the table field names is the first one you need to put in the values. If you put:

sql = &quot;INSERT INTO tablename ( username, useremail ) VALUES ('&quot; & useremail & &quot;', '&quot; & username & &quot;')

Then it would be telling userEmail variable to insert it's information into the username field, and telling the username variable to insert it's information into the useremail field. This would cause problems for you.

Hope that makes sense!
-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top