一个搜索框用于多个搜索查询

时间:2013-02-07 12:30:55

标签: php mysql sql

所以我有一个搜索框,可以搜索输入的内容(手机品牌,型号,操作系统和价格下降),并根据是否符合搜索条件,在每个框中显示每个结果。但是,当我添加多个查询时,其中两个中的一个工作,其余的则不工作。代码如下:

mysql_select_db("bg29li", $con);
$phonesearch = $_POST['phonesearch'];

$sql = "SELECT * FROM `phones` JOIN manufacturer USING (ManufacturerID) JOIN operatingsystem USING (OSID) WHERE Price <='$phonesearch' ORDER BY `Price` DESC";
$sql = "SELECT * FROM `phones` JOIN manufacturer USING (ManufacturerID) JOIN operatingsystem USING (OSID) WHERE Model LIKE '%$phonesearch%'";
$sql = "SELECT * FROM `phones` JOIN manufacturer USING (ManufacturerID) JOIN operatingsystem USING (OSID) WHERE OSName LIKE '%$phonesearch%'";
$sql = "SELECT * FROM `phones` JOIN manufacturer USING (ManufacturerID) JOIN operatingsystem USING (OSID) WHERE Name LIKE '%$phonesearch%'";

$result = mysql_query($sql);

echo "<h2>Search Results:</h2>";
while($row = mysql_fetch_array($result))
{ ?>

在此示例中,只有底部查询似乎有效(使用与输入的名称相同的品牌名称获取电话)。我想知道是否有人可以帮我解决这个问题?

感谢。

1 个答案:

答案 0 :(得分:0)

您正在覆盖变量$sql。将其减少为一个查询,如下所示:

SELECT *
FROM `phones`
JOIN manufacturer USING (ManufacturerID)
JOIN operatingsystem USING (OSID)
WHERE (Price <='$phonesearch')
  OR (Model LIKE '%$phonesearch%')
  OR (OSName LIKE '%$phonesearch%')
  OR (Name LIKE '%$phonesearch%')
ORDER BY `Price` DESC