asp ?
- Started
- Last post
- 13 Responses
- CX
Whay am I having trouble using Request.Form in a SQL string?
EX: strSQL = "SELECT * FROM reports WHERE title = Request.Form("title")"
- letgo™0
make the form request a variable
Dim Title
Title = (Request.Form("Title"))Then just call the variable in your query
- gabriel_pc0
you need to actually append the form results to the sql string. like this:
strSQL = "SELECT * FROM reports WHERE title = " & Request.Form("title")
- tfs__mag0
yeah what gabriel said... strSQL = "SELECT * FROM reports WHERE title = " & Request.Form("title")
although i usually do it slightly different
"SELECT * FROM reports WHERE title='" & request.form("title") & "'"
either way will work, i just do it that way so i can use it with numeric fields too.
- mitsu0
that's right, you need the single quotes, unless 'title' is a number.
sql = "SELECT * FROM reports WHERE title='" & request.form("title") & "'"
- CX0
I suck:
I do this:
strSQL = "SELECT * FROM reports WHERE title = " & Request.Form("title")
and get this:
Syntax error (missing operator) in query expression 'title ='
- mitsu0
response.write out Request.Form("title") to make sure it actually contains something:
Response.Write(Request.Form("tit...
- CX0
It does, I use it in other places. I didnt see your first post when I posted mitsu. Ill try that.
- CX0
Thanks, that worked. On more thing: whats the best way to check for a recordcount in asp? I currently have:
IF objRS.recordcount THEN
For my previous SQL statement.
- mitsu0
not sure what you're asking, but yes objrs.recordcount is the right property to check, but you'll need to scroll back in the recordset if you want to loop though that recordset later by using objrs.movefirst
- CX0
Here is my SQL:
strSQL = "SELECT * FROM reports WHERE title='" & request.form("title") & "'"
Then later in the code:
IF objRS.recordcount THEN
The idea is if theres already a record of that title then give a duplication error message.
- mitsu0
if you wanting to see if that recordsset created fromt he strSQL statement returned more than 1 record just do this:
if objrs.RecordCount > 1 then
Response.Write("duplicate!")
end if
- tfs__mag0
you can use COUNT in another SQL query to fetch the record count as well...
"SELECT COUNT(*) as recordcount FROM reports WHERE title='" & request.form("title") & "'"
then your recordcount is stored in objrs("recordcount")
- mitsu0
yeah, that's another way, the trade off being more memory usage but not having to reset your recordset cursor.
either way.