Skip to main content
GameDev.net gamedev.net
🔒 Locked

[web] ASP: Insert into command

Started by boolean Jan 17, 2005 at 12:04 AM 3 replies 550+ views
Original Post
boolean
boolean
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
nts
nts
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 &#106avascript 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 &#106avascript get executed when the user clicks &#111;n the link. DoRating.aspx will be able to access the form data &#111;n the first form. You can pass any strings along with the link aswell if needed.<br><br>HTH
evolutional
evolutional
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" )%>


boolean
boolean
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:

Set adocon = OpenDatabase()

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?
evolutional
evolutional
adocon is what I normally call my ado connection object, OpenDatabase calls your usual function to open your database connection :)

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

adocon = Server.CreateObject("ADODB.Connection")
adocon.Open "your_connection_string"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.