Jag bygger en sida där man kan söka ex:
jag är [val] som söker [val] och som bor i [val] och som har status [val]
problemet är att jag med denna kod
<%varLan = Request.Form("selCommunity")
varSex = Request.Form("selSex") 'Jag är
varSex1 = Request.Form("selSex1") 'som söker
varInloggad = Request.Form("selInloggad") 'och som bor i
varStatus = Request.Form("selStatus") 'och som har status
if varLan <> "" then
[b]set RS = objConn.Execute("Select distinct * From tblUsers where community = '" & varLan & "' and inloggad = "& varInloggad &" and statusItem = '"& varStatus &"' and Sex = '"& varSex &"' and Sex = '"& varSex1 &"' ORDER BY vip = 1 desc")[/b]
får detta
[MySQL][ODBC 3.51 Driver][mysqld-4.0.25-max]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'and statusItem = 'Söker' and Sex = 'M, M' and Sex = '' ORDER BY
När jag skriver ut denna del set rs = objConn.Execute("Select * From tblUsers where Sex='"& varSex &"' OR community='"& varLan &"' OR statusItem ='"& varStatus &"' OR anvbild ="& varBild &" OR inloggad="& varInloggad &"")
får jag
Select * From tblUsers where Sex='F' OR community='' OR statusItem ='' OR anvbild =1 OR inloggad=
och felmeddelande
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[MySQL][ODBC 3.51 Driver][mysqld-4.0.25-max]You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
/Search/detailsearch.asp, line 98
Då jag testade med att inte söka i alla fält så ser det rätt ut men det funkar ändå inte, VAD är fel, Rad 98 är fetmarkerad
<%if not Request.Form("selCommunity") = "" then
varLan = Request.Form("selCommunity")
end if
if not Request.Form("selSex") = "" then
varSex = Request.Form("selSex")
end if
if not Request.Form("selStatus") = "" then
varStatus = Request.Form("selStatus")
end if
if not Request.Form("selBild") = "" then
varBild = Request.Form("selBild")
end if
if not Request.Form("selInloggad") = "" then
varInloggad = Request.Form("selInloggad")
end if%>
</td>
</tr>
<%if request("do")="search" and request("type")="detail" and request("r")="1" then
set rs = objConn.Execute("Select * From tblUsers where Sex='"& varSex &"' OR community='"& varLan &"' OR statusItem ='"& varStatus &"' OR anvbild ="& varBild &" OR inloggad="& varInloggad &"")%>
Dim SqlQuery
Dim SqlWhereClause
' Bestäm vilka parametrar som ska ingå i frågan.
If Not Request.Form("selCommunity") = "" Then
SqlWhereClause = " community = '" & Request.Form("selCommunity") & "' OR"
End If
If Not Request.Form("selSex") = "" Then
SqlWhereClause = " sex = '" & Request.Form("selSex") & "' OR"
End If
...
' Ta bort sista tre tecknen ur frågan.
If SqlWhereClause <> "" Then
SqlWhereClause = Mid(SqlWhereClause, 0, Len(SqlWhereClause) - 3)
End If
' Bygg ihop WHERE-klausulen i den fullständiga sql-frågan.
SqlQuery = "SELECT * FROM tblUsers WHERE (inloggad = " & varInloggad & ") AND (" & SqlWhereClause & ")"
' Kör frågan.
set rs = objConn.Execute(SqlQuery)
...
Var ett tag sedan jag skrev vbscript så det kan ha smugit in några fel. Dessutom har jag inte lagt in några felkontroller över huvud taget (mot sql injections, när SwlWhereClause är tom etc).
SqlQuery = "SELECT * FROM tblUsers WHERE (inloggad = " & varInloggad & ") AND (" & SqlWhereClause & ")"
och hur skriver man färdigt resten av frågan med bild ,sex, status och community
SqlQuery = "SELECT * FROM tblUsers WHERE (inloggad = " & varInloggad & ") AND (" & SqlWhereClause & ")"