Click to See Complete Forum and Search --> : Selecting dynaminc fields in an SQL statement


4dam
11-04-2005, 01:31 PM
Hi,

I have a one record table, part of which consists of three fields which store image names.

menu_image_1, menu_image_2 and menu_image_3

To delete either of these images from the record, I am attempting to use the script below. The querystring "image" contains the field name of the appropriate image, and I am trying to dynamically insert it into the Query, but it keeps giving me a syntax error message:

<%

Dim empty_value
empty_value = null

Set rs_deleteimage = Server.CreateObject("ADODB.Recordset")
rs_deleteimage.ActiveConnection = MM_conn_db_STRING

rs_deleteimage.Source = "UPDATE tbl_menu SET ' " & request.querystring("image") & " ' = '" & empty_value
rs_deleteimage.CursorType = 0
rs_deleteimage.CursorLocation = 2
rs_deleteimage.LockType = 1
rs_deleteimage.Open()

response.Redirect("../control/menu_admin.asp")

rs_deleteimage.Close()
Set rs_deleteimage = Nothing
%>

What is the correct syntax for including this querystring in the query?

Thanks,

Adam

Giskard
11-04-2005, 01:37 PM
The line:
rs_deleteimage.Source = "UPDATE tbl_menu SET ' " & request.querystring("image") & " ' = '" & empty_value

should read:
rs_deleteimage.Source = "UPDATE tbl_menu SET " & request.querystring("image") & " = '" & empty_value & "'"

If "image" is the name of the field then you can't have quotes around it. Also you don't have a closing quote for the empty_value variable.

4dam
11-04-2005, 10:50 PM
Thanks,

That worked a treat. I had tried it without the quotation marks, so it must've been the one missing at the end that caused it.

But I had used that empty_value item in another query, with static values, without quotation marks at the end and it had worked. Strange.

Anyway, thanks again,

Adam