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!

inserting records into table 1

Status
Not open for further replies.

Dashsa

Programmer
Aug 7, 2006
110
US
Hello,
This is simple I know, but I am have an issue when trying to insert data into a SQL DB.

I am collecting data from a user and then inserting the data.
I think I am connecting to the DB correctly but I can seem to add the records.
Thanks for the help

Code:
<%
 firstName=Request.Form("Fname")
 LastName=Request.Form("Lname")
 DateOfBirth =Request.Form("dob")
 HomePhone =Request.Form("homePhone")

dim connectionString
connectionString="PROVIDER=MSDASQL ;DRIVER={SQL Server};"
connectionString = connectionString & "SERVER=xx.xx.xx.xx; DATABASE=xxxxxxx;"
connectionString = connectionString & "UID=xxxxxxx; PWD=xxxxxxx;"

dim sqlCode
sqlCode="INSERT INTO ContactRequests(FirstName,LastName,DateOfBirth,HomePhone)"
sqlCode=sqlCode & "VALUES"
sqlCode=sqlCode &"(firstName)"
sqlCode=sqlCode &"(LastName)"
sqlCode=sqlCode &"(DateOfBirth)"
sqlCode=sqlCode &"(HomePhone)"

dim Conn
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open connectionString



dim recordSet1
SET recordSet1 = Conn.Execute(sqlCode)





Response.Redirect "completed.html"
%>
 
Nope. You sql syntax is wrong. To insert a row in to a table, the syntax is this...

Code:
INSERT INTO ContactRequests(FirstName,LastName,DateOfBirth,HomePhone)
VALUES (firstName,LastName,DateOfBirth,HomePhone)

That is just the general syntax. Since your column data types appear to be strings (and a date), you'll want to use single quotes, like this...

Code:
INSERT INTO ContactRequests(FirstName,LastName,DateOfBirth,HomePhone)
VALUES ('Barack','Obama','1/1/1960','1-800-DEM-OCRAT')

You code needs to build a string that looks like the above. So...

Code:
dim sqlCode
sqlCode="INSERT INTO ContactRequests(FirstName,LastName,DateOfBirth,HomePhone)"
sqlCode=sqlCode & "VALUES("
sqlCode=sqlCode &"'" & firstName & "',"
sqlCode=sqlCode &"'" & LastName & "',"
sqlCode=sqlCode &"'" & DateOfBirth & "',"
sqlCode=sqlCode &"'" & HomePhone & "')"

Also... this query won't return anything, so you don't need to mess around with recordsets.

Replace:
dim recordSet1
SET recordSet1 = Conn.Execute(sqlCode)

With:

Call Conn.Execute(sqlCode)

Lastly... you'll want to learn about [google]SQL Injection[/google].

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks a bumch i'll give it a whirl!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top