0

I'm new to classic asp and i'm completely stuck on inserting values into an SQL database when a user clicks on a submit button. So far I've got the following which hopefully isn't a million miles away. Appreciate any help given. I've checked the names of the tables and columns. I feel like i may be missing out a connection, but i've got select statements that bring back ID's from the database no problem.

Thanks

<%
    getSub = Request.Form("Submit")
    if getSub = "Register" then

    Set rsTmp = Server.CreateObject("ADODB.Recordset")
    SQL = "INSERT INTO tb_table1 (tbtable1_id)"
    SQL = SQL & " VALUES ('1')"
    rsTmp.Open SQL  
    rsTmp.Close
    End if

%>
Brien Foss
  • 3,336
  • 3
  • 21
  • 31
cw2504
  • 43
  • 1
  • 7
  • 2
    If you intend on writing CRUD operations to interact with your database, [consider this example](https://stackoverflow.com/a/22729750/692942). – user692942 Apr 05 '18 at 07:57
  • You probably need to recover the submitted values using `Request.Form` or `Request.QueryString` and replace the values in sql string. – Ricardo Pontual Apr 05 '18 at 18:53
  • @RicardoPontual great idea, let’s open them up to SQL Injection. Why didn’t I think of that?? Oh yeah, I pointed them to an example that avoids that very scenario. – user692942 Apr 05 '18 at 22:00
  • Lankymarts link is the way to go. Still I just want to tell you why the exact code you have above wont work. You need as you say a connection to the database, so the open cmd should be: `rsTmp.Open SQL, CONN_STRING` Also, when doing an insert through a recordset you cant close it, so you'll get an error on this row aswell, so remove that row. But again, rewrite the code according to Lankymarts link is the best solution... – Sourcery Apr 10 '18 at 08:19

1 Answers1

-1

do it

getSub = Request.Form("Submit")

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

if getSub = "Register" then
    SQL = "INSERT INTO tb_table1 (tbtable1_id)"
    SQL = SQL & " VALUES ('1')"
    Conn.Execute SQL
End if

Conn.Close
Set Conn = Nothing