# [web] ASP: Insert into command

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all I have an ASP page that will be used for rating items on the site. I have set it up so that you click a link called 'Vote' which takes you to a new screen asking you to click a link from 1 to 5. I am then planning on taking the ItemID from the page you clicked the vote link, and the rating from the page you clicked 1 to 5, and then insert this into a table called Ratings (with three tables called RatingID, ItemID, Rating). The problem is, I am not to sure how to use the INSERT INTO command to do this. I can only find a few tutorials which seem to cover very specific databases, and do the them through forms (I am trying to do it through a link). I would of thought that there would be heaps of tutorials on the internet on this subject, but I am having trouble finding an answer. If anyone can give me some hints on how to do this, or point me to a relevant site, you are a champion. Thanks

##### Share on other sites
If you are talking about just the SQL syntax side of it then something like
INSERT INTO Ratings (RatingID, ItemID, Rating)VALUES (value1, value2, value3);

if you are talking about clicking on the link and having that happen then you can force a post back on the page through javascript or have it postback to another ASP file, probably cleaner (shown below).

something like
window.document.forms[0].action='DoRatings.aspx';window.document.forms[0].submit();

and have that javascript get executed when the user clicks on the link. DoRating.aspx will be able to access the form data on the first form. You can pass any strings along with the link aswell if needed.

HTH

##### Share on other sites
Assuming you have a form set up:

[source<%If Request.QueryString("ItemID") <> "" Then  pItemID = CLng( Request.QueryString("ItemID") )Else  pItemID = 0End If%><form action="dorating.asp" method="post">   <select name="rating">     <option value="1">1</option>     <option value="2">2</option>     <option value="3">3</option>     <option value="4">4</option>     <option value="5">5</option>   </select>   <input name="ItemID" type="hidden" value="<%= pItemID %>"  />   <input type="submit" value="Rate Item" /></form>

When the user selects the rating and submits the form, the information will be passed to dorating.asp via POST.

So, in dorating.asp:

<%If Request.Form("ItemID") <> "" Then  pItemID = CLng( Request.Form("ItemID") )Else  pItemID = 0End IfIf Request.Form("rating") <> "" Then  pRating = CInt( Request.Form("rating") )Else  pRating = 0End IfIf pRating <= 0 OR pItemID <= 0 Then  ''' There was an error - make sure you handle this in your own way!  ''' This script will just stop  Response.EndEnd If'''' Otherwise, test rating for bounds:If pRating > 5 Then   ''' Someone tried to hax0r the script by passing rating above 5...   ''' This script just stops - handle in your own way   Response.EndEnd If''' Script should be ok now - insert into database''' SQL assumes RatingID is an AutoNumbersqlInsert = "INSERT INTO Ratings( ItemID, Rating ) VALUES ( " & pItemID & ", " & pRating & ");"''' Do your database opening thing here....Set adocon = OpenDatabase()''' Execute insert statementadocon.Execute( sqlInsert )''' close database connectionadocon.close''' All done, go back to whatever pageResponse.Redirect( "viewratings.asp" )%>

##### Share on other sites
Thanks guys.

Evo, lucky you posted that. There were a few things in there I did not even think of (like checking to see if the value is above 5, to stop people typing rating=1000000 into the URL)

One bit I am stuck on, which is extremely n00bish, is this bit here:

I am not to sure what to put in here, since I am assuming I have to type something to setup a connection, but I cant find anywhere else that I made a connection to the DB that looks like this. Any advice?

##### Share on other sites

I put that in there as a placeholder for code such as:

##### Share on other sites

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628736
• Total Posts
2984457

• 25
• 11
• 10
• 16
• 14