The ASP On Line Course:
Doing an Insert with ASP
The only unhappy student I have ever had on this site was trying to insert data into a message board database for a homework assignment. Here is what she was trying to do. Hopefully it should be obvious to you why it failed!
Her homework assignment was to create a "Laura's Winter Activity MessageBoard" application. In essence she had a form where a user typed in the message, and she wanted this message added into her database. So really, the 'add' page only had one function - to take the data from the form and insert it into the database table.
I sent her a message explaining that this is the normal code to alter data in a database table:
'INSERT NEW ENTRY
Set objCmd4 = Server.CreateObject ("ADODB.Command")
SQLTxt = "update traffic set hit_count = hit_count + 1 where counter = 142;"
objCmd4.ActiveConnection = strConnect
objCmd4.CommandType = &H0001
objCmd4.CommandText = SQLTxt
Set objCmd4 = Nothing
In this case, you are updating a traffic counter by one for page #142. You could put something like this on the bottom of each page in your website, changing the ID # for each page, and keep running live traffic counters for your site.
Her response was that her code to insert looked like this:
objCommand.CommandText = "Insert into Activity values(" & request("Aname") & ", '" & request("Acontact") & "','" & request("Apw") & "','" & request("Amsg")& "')"
Those were the ONLY two lines of code in her insert page. Compare her two lines of code to the functional code which I sent her. What is the difference between the two sets of code? She has one statement which assigns a value to the CommandText member of the objCommand object (which is never created to begin with). Her second line is a response.write statement - i.e. it simply puts something on the user's screen to read. What key statements are missing in order for her code to execute against the database?
If you found the answer, congratulations!!
ASP Course and Code | HTML Tips, Code & Help | Java Code & Help | Gaming Walkthroughs