我有一个mysql查询,用于查找属于某个组的某个位置的库存项目。因此,这要经历 4 个级别的while loops
.不,我已经让用户能够选择他们想要查看股票的位置。这是使用数组中的ajax发送的复选框来实现的。数组exploded in PHP
使用 $offices = explode(",", $locations);
。但是现在我想使用在我的 mysql 查询中选择的位置。
$location
以location1, location2, location3, location4
的形式出现
//selecting all locations using the statement below, however i want to select the locations that where selected by user.
$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where 'wanted locations');
while($row3 = mysql_fetch_array($sql4)) {
$curr_location = $row3[0];
$sql3 = mysql_query("select Quantity from Stock_Management where Book_ID = '$curr_book' and Location_ID = '$curr_location'");
while($row3 = mysql_fetch_array($sql3)) {
echo "<td>".$row3[0]."</td>";
}
}
echo "</tr>";
我想根据用户选择的位置选择位置,现在可以使用 for 循环来实现这一点,我不知道如何将其包含在我的 sql 查询中!
$locations = mysql_real_escape_string($locations);
$locations = str_replace(",","','",$locations);
$sql = "select OfficeID, OfficeTitle from Office WHERE location in ('$locations')";
$offices = explode(",", $locations);
$loc = implode("','", $offices);
这有助于创建变量$loc
location1','location2',location3
$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('$loc')");
这会将 mysql 查询创建为:
$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('location1','location2',location3')");
,这解决了现在的目的。
SELECT OfficeID, OfficeTitle FROM Office WHERE OfficeID IN ( $locations );
??
另外,查找mysql_real_escape_string
和"关注点分离">