2016-12-24 65 views
1

这是使用SQL Server 2012数据库的IIS 7.5经典ASP网站。在SQL语句中使用通过AJAX的2个值

我需要帮助。我通过一个函数传递2个值到另一个经典的ASP页面,这反过来将在SQL语句中使用这些值返回一个值

当我使用它时,它不会返回值,但是当我手动输入一个值时,如UnitPieces的'7'和City的'Los Angeles',它从SQL Server数据库返回适当的数据。

以下是我与

<form> 
<select id="shiptoarea" name="shipcost" class="required" onchange="shipping(this.value)"> 
<option disabled="disabled" selected="selected" value="">--Choose One--</option> 
<option value="San Francisco">San Francisco</option> 
<option value="Los Angeles">Los Angeles</option> 
<option value="New York">New York</option> 
<option value="San Diego">San Digeo</option> 
</select> 
</form> 
<p id="shippingcost"></p> 

,它的值发送到这个AJAX功能工作(注:<%= vcItems%>在购物车中的商品数量)

function shipping(str) { 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
      document.getElementById("shippingcost").innerHTML = this.responseText; 
    }; 
    xmlhttp.open("GET", "shiptoamount.asp?y=<%=vcItems%>&q=" + str, true); 
    xmlhttp.send(); 
} 
</script> 

这是运行SQL查询的页面

Dim Connection 
Dim ConnString 
Dim Recordset 
Dim SQL 
Dim qq 
Dim yy 

qq = (request.querystring("q")) 
yy = (request.querystring("y")) 

ConnString = "DRIVER={SQL Server};SERVER=SQLServerName;UID=username;" & _ 
"PWD=password;DATABASE=databasename" 

SQL = "SELECT FlatShipCost FROM ShipRates WHERE UnitPieces = "&yy&" AND City = "&qq 

Set Connection = Server.CreateObject("ADODB.Connection") 
Set Recordset = Server.CreateObject("ADODB.Recordset") 

Connection.Open ConnString 

Recordset.Open SQL,Connection 

If Recordset.EOF Then 
    Response.Write("No records returned.") 
Else 
    if NOT Recordset.Eof then 
     Response.write (request.querystring("q")) 
     Response.write (Recordset("FlatShipCost")) 
    end if 
End if 

Recordset.Close 
Set Recordset = nothing 
Connection.Close 
Set Connection = nothing 

奇怪的是,如果我手动

Response.write (request.querystring("q")) 

Response.write (request.querystring("y")) 

它会显示在下拉选择为City的HTML,也从<%,在车的正确数量的项目= vcItems%>。所以这些值会进入运行查询的页面,但我想我可能会在SQL语句中做错了什么。请帮忙。在此先感谢

+0

(https://xkcd.com/327/) –

回答

0

也许你只是错过了引号的字符串QQ传递到城市的条件:[注意小鲍比表]

SQL = "SELECT FlatShipCost FROM ShipRates WHERE UnitPieces = "&yy&" AND City = '"&qq&"';" 
+0

谢谢你的回应,Falco。我尝试了你的建议,并没有骰子:( – Windows95

+0

检查!我有一个错字!这是它的先生!非常感谢你,法尔科! – Windows95