PDA

View Full Version : Insert to Database


dagaffer
01-14-2003, 09:00 PM
How would I insert a couple of details from a form into a database?

scroots
01-14-2003, 09:10 PM
it depends on many things
if you set your form action to post it to say process.asp in process.asp you could have (my code might not be the best as it is done by me.)

<%
'Dimension variables
Dim adoCon 'Holds the Database Connection Object
Dim rsAddComments 'Holds the recordset for the new record to be added to the database
Dim strSQL 'Holds the SQL query for the database

'Create an ADO connection odject
Set adoCon = Server.CreateObject("ADODB.Connection")

'Set an active connection to the Connection object using a DSN-less connection
'change database.mdb to the required file
adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database.mdb")



'Create an ADO recordset object
Set rsAddComments = Server.CreateObject("ADODB.Recordset")

'Initialise the strSQL variable with an SQL statement to query the database
'you will need to change the names of fields
strSQL = "SELECT tblComments.F, tblComments.T, tblComments.D, tblComments.Di, tblComments.M, tblComments.H, tblComments.Ti FROM tblComments;"

'Set the cursor type we are using so we can navigate through the recordset
rsAddComments.CursorType = 2

'Set the lock type so that the record is locked by ADO when it is updated
rsAddComments.LockType = 3

'Open the tblComments table using the SQL query held in the strSQL varaiable
rsAddComments.Open strSQL, adoCon
'Tell the recordset we are adding a new record to it
rsAddComments.AddNew

'Add a new record to the recordset
'the first par in ("here") is your database field name
'the second part is the form fieldname
rsAddComments.Fields("F") = Request.Form("fro")
rsAddComments.Fields("T") = Request.Form("t")
rsAddComments.Fields("D") = Request.Form("des")
rsAddComments.Fields("Di") = Request.Form("dis")
rsAddComments.Fields("M") = Request.Form("mag")
rsAddComments.Fields("H") = Request.Form("hei")
rsAddComments.Fields("Ti") = Request.Form("tfl")


'Write the updated recordset to the database
rsAddComments.Update

'Reset server objects
rsAddComments.Close
Set rsAddComments = Nothing
Set adoCon = Nothing

'Redirect to the a page
Response.Redirect "guestbook.asp"
%>

comments included for you

scroots

tsbarnes
01-14-2003, 09:11 PM
Here is a great example:
http://www.asp101.com/samples/form_to_db.asp

Asp101.com is a great place to start if you are just learning asp!
http://www.asp101.com/samples/index.asp

Hope this helps,

Tsbarnes

dagaffer
01-14-2003, 09:16 PM
Thanx both of ya:thumbsup:

dagaffer
01-14-2003, 10:21 PM
INSERT INTO tblLoginInfo (username, password) VALUES ('" & user1 & "', '" & pass1 & "');
Is there anything wrong with this? When I use it I get a syntax error. Help! (please:o)
Thanx, gaffer:thumbsup:

whammy
01-15-2003, 12:43 AM
What field types are they?

I know in access, either username or password is a reserved word, but I can never remember which one. Ahh, here you go, from my register.zip file (basic login/registration script):

Dim username ' user is a reserved word in Access
Dim pass ' password is a reserved word in Access

These appear to be the case, since I also got syntax errors for no apparent reason when using access as a database. I could be wrong, but I changed the field names, and it works. Go figure. :)

P.S. I rewrote my basic login/registration script to be even simpler (but it still requires someone to respond to an email to be registered):

http://www.solidscripts.com/downloads/register.zip

aCcodeMonkey
01-15-2003, 09:55 AM
Easiest way to confuse.... Uh, use reserved words in Access is to bracket the field names

SQL = "INSET INTO Accounts (username,[password]) Values('" & myVar & "','" & myVar1 & "')"

This also works for field names/aliases with spaces

SELECT First+' '+Last AS [User Name] FROM Users

As Mom once warned.. If you aren't sure, wrap it.

Hope this helps :cool:

dagaffer
01-15-2003, 06:54 PM
Ah, Thanx for that. I'll try to bracket the field names 1st, if that doesn't work I'll rename them.
I'll look at your simpler script whammy (& see if I can understand it:rolleyes:), but I really don't need a reply, if someone gives me a fake e-mail address, it's their loss, I'll either live with it or delete it... It doesn't really need to be anything formal. Thanx anyway!:thumbsup: