I have this code that I know is right and I have checked it for hours BUT it results in the wrong values. Would you please look and see if I am blocking something out. I am trying to build up an SQL string for a database with values from a form and the values look right but the string is wrong.
Here is the code:
For i= 1 to count
FoodPs = "FoodPs" & i
Response.Write("<br>")
Response.Write("Food is:")
Response.Write(Session("FoodN")(i)) 'CORRECT
Response.Write("<br>")
Response.Write("Preference is:")
Response.Write(request.form(FoodPs)) 'CORRECT
Session("FoodP")(i) = request.form(FoodPs) 'here I put it in the array
Response.Write("<br>")
response.write("FoodPs name="& FoodPs)
mSQL="INSERT INTO Foods("
mSQL = mSQL & "Family,"
mSQL = mSQL & "PersonID,"
mSQL = mSQL & "Food,"
mSQL = mSQL & "Preference)"
mSQL = mSQL & "Values("
mSQL = mSQL & "'" & Replace(Session("GuestID"),"'","''") & "', "
mSQL = mSQL & "'" & Replace(Session("Fcount"),"'","''") & "', "
mSQL = mSQL & "'" & Replace(Session("FoodN")(i),"'","''") & "', "
mSQL = mSQL & "'" & Replace(Session("FoodP")(i),"'","''") & "')" ' ' HERE I USE THE ARRAY YET THE VALUE IS WRONG!
Response.Write("<br>")
Response.Write(mSQL) 'change this to execute
mSQL = ""
Next
IT RESULTS IN THIS OUTPUT:
Food is:tomato
Preference is:3
FoodPs name=FoodPs1
INSERT INTO Foods(Family,PersonID,Food,Preference)Values('9', '1', 'tomato', '3')
Food is:carrot
Preference is:3
FoodPs name=FoodPs2
INSERT INTO Foods(Family,PersonID,Food,Preference)Values('9', '1', 'carrot', '2')
The last number is '2' when it should be '3' because it is the same value " request.form(FoodPs)" which is put into a Session array,"Session("FoodP")(i)" because the request.form give an error when added to the string.
After explaining that long post it made me think why am I using a session variable at all. I used a local variable and that did the trick. Sorry if you took time puzzling over this but writing it made me see the problem.
Bookmarks