好吧,我在一个名为 week-select 的页面上有一个多选下拉菜单,它的选择通过 ajax 传递到我的 php 页面。
我可以很好地获取数据,但是当查询运行时它没有正确完成。
我有这个:
//Deal with Week Array
$weekFilter = $_GET['week']; /*This is fine, if it's 1 week the query works great (weeks are numbered 12-15), but if it is 2 weeks the result is formatted like this 12-13 or 13-14-15 or whichever weeks are selected*/
$weekFilter = str_replace("-",",",$weekFilter); /*This works to make it a comma separated list*/
.../*I deal with other variables here, they work fine*/
if ($weekFilter) {
$sql[] = " WK IN ( ? ) ";
$sqlarr[] = $weekFilter;
}
$query = "SELECT * FROM $tableName";
if (!empty($sql)) {
$query .= ' WHERE ' . implode(' AND ', $sql);
}
$stmt = $DBH->prepare($query);
$stmt->execute($sqlarr);
$finalarray = array();
$count = $stmt->rowCount();
$finalarray['count'] = $count;
if ($count > 0) { //Check to make sure there are results
while ($result = $stmt->fetchAll()) { //If there are results - go through each one and add it to the json
$finalarray['rowdata'] = $result;
} //end While
}else if ($count == 0) { //if there are no results - set the json object to null
$emptyResult = array();
$emptyResult = "null";
$finalarray['rowdata'] = $emptyResult;
} //end if no results
如果我只选择一周,它会很好地显示适当的数据。
如果我选择多个选项(例如第 12 周、第 14 周和第 15 周),它将运行查询,但仅显示第 12 周。
当我在 SQL 中手动输入查询时,我想象这个查询是如何被输入的——它运行并显示适当的数据。因此,如果我将 SELECT * FROM mytablename WHERE WK IN ( 12, 14, 15 ) 放入,它就会得到我想要的。
我不知道为什么我的查询在这里没有正确执行。有任何想法吗?
**编辑:在将多个选择中的数组传递到后端之前,我在前端使用 javascript 将数组设为字符串。