Sign in to follow this  

[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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Assuming you have a form set up:

[source
<%
If Request.QueryString("ItemID") <> "" Then
pItemID = CLng( Request.QueryString("ItemID") )
Else
pItemID = 0
End 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 = 0
End If

If Request.Form("rating") <> "" Then
pRating = CInt( Request.Form("rating") )
Else
pRating = 0
End If

If 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.End
End 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.End
End If

''' Script should be ok now - insert into database

''' SQL assumes RatingID is an AutoNumber
sqlInsert = "INSERT INTO Ratings( ItemID, Rating ) VALUES ( " & pItemID & ", " & pRating & ");"

''' Do your database opening thing here....
Set adocon = OpenDatabase()
''' Execute insert statement
adocon.Execute( sqlInsert )
''' close database connection
adocon.close

''' All done, go back to whatever page
Response.Redirect( "viewratings.asp" )
%>



Share this post


Link to post
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:

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?

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this